As the titles says I can't seem to build the project with OpenGL and Glut.
I get Undefined reference errors for OpenGL functions.
I tried doing :
project(testas)
find_package(OpenGL)
find_package(GLUT)
add_executable(testas main.cpp)
But that doesn't work.
Any suggestions?
In recent versions of CMake (3.10+) there is a new way to use OpenGL using a so-called IMPORTED target:
At the moment the only practical difference seems to be on Linux (where GLVND is used if available), but presumably this solution should be more future-proof, as CMake has more information about your intentions and other tools parsing your CMakeFiles will better understand the dependency tree.
find_package(OpenGL)
will find the package for you, but it doesn't link the package to the target.To link to a library, you can use
target_link_libraries(<target> <item>)
. In addition, you also need to set theinclude directory
, so that the linker knows where to look for things. This is done with theinclude_directories
.An example
CMakeLists.txt
which would do this looks something like this:If
OpenGL
is a necessity for your project, you might consider either testingOpenGL_FOUND
after thefind_package(OpenGL)
or usingREQUIRED
, which will stopcmake
ifOpenGL
is not found.For more information and better examples:
In particular, the
CMake wiki
andcmake and opengl
links should give you enough to get things working.I use these two cmake files to build my OpenGL projects, and they all work well.
I only test these two cmake files under Deepin Linux. Deepin Linux is a Chinese grown Linux system like Ubuntu or from Debian.