2

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?

1
  • You incorrectly set GLUT_LIBRARY variable to be pointed to glfw library, but linking uses variable GLFW_LIBRARY. As for undefined references to GL functions in SOIL library: linking order has a sence. Library-consumer should come before library-provider in target_link_libraries call. (There is definitely a question on SO which describes that, but I cannot find it.) Commented Nov 4, 2016 at 18:27

2 Answers 2

2

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/

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

Because I am using windows I would need to use hints to specify the directories of the libraries?
Not for OpenGL and GLEW which should be installed in the directories of your compiler (MinGW ?). For GLFW and SOIL, I guess you may install them the same way, but if they are installed in an "arbitrary" directory, let's say in C:/Users/John/OneDrive/OpenGL Projects/OpenGL/Lib_files, you will have to tell CMake about this (you should avoid whitespaces in paths ; if you have whitespaces in a path, wrap it with quotes in the set command: set(FOO "/path/with a/whitespace"))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.