Nick Evans
Nick Evans

Reputation: 39

Undefined Reference error using OpenGL in CLion

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

"C:\Program Files (x86)\JetBrains\CLion 2016.2.1\bin\cmake\bin\cmake.exe" --build C:\Users\Nick\.CLion2016.2\system\cmake\generated\cs455opengl-9b23e7f0\9b23e7f0\Debug --target all -- -j 4
Scanning dependencies of target cs455openGL
[ 50%] Building CXX object CMakeFiles/cs455openGL.dir/main.cpp.obj
[100%] Linking CXX executable cs455openGL.exe
CMakeFiles\cs455openGL.dir/objects.a(main.cpp.obj): In function `Z4initv':
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:12: undefined reference to `glClearColor@16'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:13: undefined reference to `glMatrixMode@4'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:14: undefined reference to `glLoadIdentity@0'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:15: undefined reference to `glOrtho@48'
CMakeFiles\cs455openGL.dir/objects.a(main.cpp.obj): In function `Z7displayv':
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:20: undefined reference to `glClear@4'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:21: undefined reference to `glColor3f@12'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:22: undefined reference to `glVertex3f@12'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:23: undefined reference to `glVertex3f@12'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:24: undefined reference to `glVertex3f@12'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:25: undefined reference to `glVertex3f@12'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:26: undefined reference to `glEnd@0'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:27: undefined reference to `glFlush@0'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\cs455openGL.dir\build.make:95: recipe for target 'cs455openGL.exe' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/cs455openGL.dir/all' failed
mingw32-make.exe[2]: *** [cs455openGL.exe] Error 1
mingw32-make.exe[1]: *** [CMakeFiles/cs455openGL.dir/all] Error 2
mingw32-make.exe: *** [all] Error 2
Makefile:82: recipe for target 'all' failed

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(cs455openGL)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)

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

set(SOURCE_FILES main.cpp)
add_executable(cs455openGL ${SOURCE_FILES})
link_directories(${OPENGL_gl_LIBRARY})
target_link_libraries(cs455openGL libfreeglut.a libfreeglut_static.a)

The libraries I linked were the only library files that freeglut came with. I've been scouring the internet for answers and no one seems to have run into this.

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?

Upvotes: 2

Views: 2790

Answers (1)

t.niese
t.niese

Reputation: 40842

This is a linking error, and tells you that the linker does not find the functions defined by the OpenGL library.

You have to add ${OPENGL_LIBRARIES} to target_link_libraries.

And for glut - and any other library - you should not use the library name (libfreeglut.a) directly, but always use the variables populated by the find_package:

target_link_libraries(cs455openGL ${OPENGL_LIBRARIES} ${GLUT_LIBRARY})

Upvotes: 5

Related Questions