Communication error in OpenGL using freeglut in CLion

So, I use freeglut to try to make some openGL stuff, but I keep getting errors saying links 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 complete this project. I thought everything was right. I moved the corresponding files to the include folder in MinGW, as well as to the bin folder, as well as to 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 to which freeglut was added.

So what am I missing? The CLI does not detect any errors before compilation. I can even enter functions in the header files provided by freeglut. Why then are these functions not defined in my program?

+8
source share
3 answers

In fact, you did not associate OpenGL with your project, so you get undefined links to OpenGL functions. Try replacing

 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 problem using CMakeLists.txt and the following program:

 #include <GL/gl.h> int main() { glClear(GL_COLOR_BUFFER_BIT); return 0; } 

and decided it with a replacement above. The solution automatically links the 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 can specify the files (files) of the OpenGL library as follows:

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

You can also add the OpenGL library directory to the search path (do this before target_link_libraries ):

 link_directories(${OPENGL_gl_LIBRARY}) 
+1
source

I struggled with the same problem and solved it by adding the 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}) 
+1
source

Reordering 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}) 
0
source

All Articles