user1031204
user1031204

Reputation: 701

undefined reference to `glfwInit' issue Clion OpenGL using cmake

I am trying to get an OpenGl project working on CLion. I am using 3 different libraries: GLEW, GLFW and SOIL.

The includes seem to work fine and everything get's found but every time I try to build I get errors:

undefined reference to `glfwInit'
undefined reference to `glfwWindowHint'
undefined reference to `glfwWindowHint'
undefined reference to `glfwWindowHint'
undefined reference to `glfwWindowHint'
undefined reference to `glfwCreateWindow'
...

C:/Users/John/OneDrive/OpenGL Projects/OpenGL/Lib_files/SOIL/lib/libSOIL.a(SOIL.o):SOIL.c:(.text+0x3e): undefined reference to `glGetString@4'
C:/Users/John/OneDrive/OpenGL Projects/OpenGL/Lib_files/SOIL/lib/libSOIL.a(SOIL.o):SOIL.c:(.text+0x72): undefined reference to `glGetString@4'
...

This is what I got in my cmake file:

cmake_minimum_required(VERSION 3.6)
project(OpenGL)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp Shader.h Shader.cpp)

set(LIBS_DIR C:/Users/John/OneDrive/OpenGL\ Projects/OpenGL/Lib_files)

set(GLEW_ROOT_DIR ${LIBS_DIR}/GLEW )
set(GLFW_ROOT_DIR ${LIBS_DIR}/GLFW )
set(SOIL_ROOT_DIR ${LIBS_DIR}/SOIL )

set(GLEW_INCLUDE_DIRS ${GLEW_ROOT_DIR}/include)
set(GLFW_INCLUDE_DIRS ${GLFW_ROOT_DIR}/include)
set(SOIL_INCLUDE_DIRS ${SOIL_ROOT_DIR}/include)

set(GLEW_LIBRARY ${GLEW_ROOT_DIR}/lib/libglew32.a)
set(GLUT_LIBRARY ${GLFW_ROOT_DIR}/lib/libglfw3.a)
set(SOIL_LIBRARY ${SOIL_ROOT_DIR}/lib/libSOIL.a)


include_directories( ${GLEW_INCLUDE_DIRS} ${GLFW_INCLUDE_DIRS} ${SOIL_INCLUDE_DIRS})

add_executable(OpenGL ${SOURCE_FILES})

target_link_libraries(OpenGL libopengl32.a ${GLEW_LIBRARY} ${GLFW_LIBRARY} ${SOIL_LIBRARY})

Once I try to build I get a sea of "undefined reference" errors for GLFW and SOIL but not for GLEW.

What am I doing wrong?

Upvotes: 2

Views: 5828

Answers (2)

Perfectionist Evil
Perfectionist Evil

Reputation: 21

I meet this problem as well, adding "glfw" at the end of the target_link_libraries() may help. I get this solution from reddit. https://www.reddit.com/r/GraphicsProgramming/comments/76rtt5/linking_glfw3_with_clion/

Upvotes: 2

rocambille
rocambille

Reputation: 15976

There are several issues in your CMake file. First:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

To ask for C++11, you may prefer setting CXX_STANDARD on your target, or CMAKE_CXX_STANDARD if you want to set it at global scope. CMAKE_CXX_STANDARD_REQUIRED can be used to make your wanted version a requirement:

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

You should not call your executable OpenGL since it's ambiguous with the library.

To link against libraries, you should rely on find_package to set the variable for each library:

find_package(GLEW REQUIRED)

Note that you didn't link against OpenGL, thus the undefined reference to glGetString. For the other undefined references, I guess your variables are set in a wrong way.

The correct configuration will be different for each package, but here is a cleaned version of your project:

cmake_minimum_required(VERSION 3.6)
project(MyOpenGLProject)

find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
find_package(GLFW REQUIRED)
find_package(SOIL REQUIRED)

set(SOURCE_FILES main.cpp Shader.h Shader.cpp)
add_executable(my_opengl_executable ${SOURCE_FILES})

target_link_libraries(my_opengl_executable ${OPENGL_LIBRARIES} GLEW::GLEW ${GLFW_LIBRARY} ${SOIL_LIBRARY})
target_include_directories(my_opengl_executable PUBLIC ${OPENGL_INCLUDE_DIR} ${GLFW_INCLUDE_DIR} ${SOIL_INCLUDE_DIR})

set_target_properties(my_opengl_executable PROPERTIES
    CXX_STANDARD 11
    CXX_STANDARD_REQUIRED ON
)

Note that GLEW declares an imported target GLEW::GLEW which embeds include directories and other parameters: you just need to link against it using taget_link_libraries.

You will have to set hint variables for GLFW and SOIL for find_package to work. The variable names should be outputed in the error log when you try to configure the cleaned version above.

Upvotes: 0

Related Questions