Linking error in OpenGL using freeglut in CLion
Asked Answered
S

3

8

So, I am using freeglut to try to do some openGL stuff, but I keep getting errors saying that references are undefined:

CMakeFiles\texture_mapping.dir/objects.a(TextureMapper.cpp.obj): In function `ZN13TextureMapper4initEv':
.../TextureMapper.cpp:20: undefined reference to `glClearColor@16'
.../TextureMapper.cpp:23: undefined reference to `glMatrixMode@4'
.../TextureMapper.cpp:24: undefined reference to `glLoadIdentity@0'
.../TextureMapper.cpp:25: undefined reference to `glOrtho@48'
CMakeFiles\texture_mapping.dir/objects.a(TextureMapper.cpp.obj): In function `ZN13TextureMapper7displayEv':
.../TextureMapper.cpp:45: undefined reference to `glClear@4'
...TextureMapper.cpp:48: undefined reference to `glColor3f@12'
...TextureMapper.cpp:49: undefined reference to `glBegin@4'
...TextureMapper.cpp:52: undefined reference to `glVertex3f@12'
...TextureMapper.cpp:53: undefined reference to `glVertex3f@12'
...TextureMapper.cpp:54: undefined reference to `glVertex3f@12'
...TextureMapper.cpp:55: undefined reference to `glVertex3f@12'
...TextureMapper.cpp:58: undefined reference to `glEnd@0'
...TextureMapper.cpp:61: undefined reference to `glFlush@0'

I am using MinGW with CLion to do this project. I thought that I got everything correctly. I moved the appropriate files into the include folder in MinGW, as well as the bin folder, and also the lib folder. Then, I have this in my CMakeLists.txt:

cmake_minimum_required(VERSION 3.3)
project(texture_mapping)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)

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

set(SOURCE_FILES main.cpp TextureMapper.cpp TextureMapper.h Vertex.h ObjParser.cpp ObjParser.h)

add_executable(texture_mapping ${SOURCE_FILES})
target_link_libraries(texture_mapping libfreeglut.a libfreeglut_static.a)

The libraries I linked were the only library files that freeglut came with.

So, what am I missing? CLion doesn't show any errors before it is compiled. I can even go into the functions in the header files provided by freeglut. Why then, are these functions not defined in my program?

Sundin answered 15/9, 2016 at 2:43 Comment(1)
Your issue has nothing to do with CLion. It's just about CMake and your environment.Unsheathe
U
1

You did not actually link OpenGL to your project, so you get undefined references to OpenGL functions. Try to replace

target_link_libraries(texture_mapping libfreeglut.a libfreeglut_static.a)

with

target_link_libraries(texture_mapping libfreeglut.a libfreeglut_static.a GL)

I reproduced your issue with your CMakeLists.txt and the following program:

#include <GL/gl.h>

int main() {
        glClear(GL_COLOR_BUFFER_BIT);
        return 0;
}

and solved it with the above replacement. The solution automatically links GL library from my library path:

$ ls -1 /usr/lib64/libGL.*
/usr/lib64/libGL.la
/usr/lib64/libGL.so
/usr/lib64/libGL.so.1
/usr/lib64/libGL.so.1.0.0

UPDATE

According to this, you have some variables to access your actual OpenGL libraries. For example, you may point to OpenGL library file(s) directly like this:

target_link_libraries(texture_mapping libfreeglut.a libfreeglut_static.a ${OPENGL_gl_LIBRARY})

Also you may add OpenGL library directory to the library search path (do it before target_link_libraries):

link_directories(${OPENGL_gl_LIBRARY})
Unsheathe answered 15/9, 2016 at 2:58 Comment(3)
I get c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: cannot find -lGL as a result of that.Sundin
@CacheStaheli I updated my answer. That should work as it takes your actual environment into account.Unsheathe
As soon as I added the link_directories call, along with the extra library (${OPENGL_gl_LIBRARY}) in target_link_libraries, it worked splendidly. Thanks!Sundin
S
2

I have struggled with the same problem and solved it by appending following lines to CMakeLists.txt:

find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
include_directories(${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES} ${GLUT_LIBRARY})
Sumba answered 26/10, 2018 at 21:22 Comment(0)
U
1

You did not actually link OpenGL to your project, so you get undefined references to OpenGL functions. Try to replace

target_link_libraries(texture_mapping libfreeglut.a libfreeglut_static.a)

with

target_link_libraries(texture_mapping libfreeglut.a libfreeglut_static.a GL)

I reproduced your issue with your CMakeLists.txt and the following program:

#include <GL/gl.h>

int main() {
        glClear(GL_COLOR_BUFFER_BIT);
        return 0;
}

and solved it with the above replacement. The solution automatically links GL library from my library path:

$ ls -1 /usr/lib64/libGL.*
/usr/lib64/libGL.la
/usr/lib64/libGL.so
/usr/lib64/libGL.so.1
/usr/lib64/libGL.so.1.0.0

UPDATE

According to this, you have some variables to access your actual OpenGL libraries. For example, you may point to OpenGL library file(s) directly like this:

target_link_libraries(texture_mapping libfreeglut.a libfreeglut_static.a ${OPENGL_gl_LIBRARY})

Also you may add OpenGL library directory to the library search path (do it before target_link_libraries):

link_directories(${OPENGL_gl_LIBRARY})
Unsheathe answered 15/9, 2016 at 2:58 Comment(3)
I get c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: cannot find -lGL as a result of that.Sundin
@CacheStaheli I updated my answer. That should work as it takes your actual environment into account.Unsheathe
As soon as I added the link_directories call, along with the extra library (${OPENGL_gl_LIBRARY}) in target_link_libraries, it worked splendidly. Thanks!Sundin
C
1

Reordering the CMakeLists.txt helped me (<name> should be replaced accordingly):

cmake_minimum_required(VERSION 3.10)
project(<name>)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lGL -lGLU -lglut")
set(CMAKE_CXX_STANDARD 17)

add_executable(<name> main.cpp)

find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
include_directories(${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES} ${GLUT_LIBRARY})
Calkins answered 9/12, 2018 at 2:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.