Using CMake to build the samples from the Red Book

I am working through the OpenGL Programming guide and have downloaded the sample code from here, but I am absolutely clueless as how to “build” the repository. I thought it was as simple as compiling the .cpp source files with g++, but I can’t figure out how to get the compiler to “find” the header files from include/ directory. And every source file seems to depend on these header files. My guess is this is what “building” takes care of as mentioned in the file:

We are using CMake as our build system. To build the samples, enter
the “build” subdirectory and enter “cmake -G “{your generator here}” …”
Project files or makefiles will be produced in the build subdirectory.
We have tested Unix makefiles (“Unix Makefiles”) and Visual Studio project files.

I realize this question isn’t so much about OpenGL, as it is about CMake. However, I am completely clueless about how to use CMake, and build systems in general. I entered the “build” subdirectory which contains just an xml document templates/, but I am not sure what the “{your generator here}” argument refers to, and this seems pretty crucial to building the system. I am trying to run this on Ubuntu 19.04 by the way. Any help would be appreciated.

On Unix, use:
cmake -G "Unix Makefiles"
Then you can run “make” to actually build the code. In fact, you can probably run “cmake” without arguments, as “Unix Makefiles” is normally the default generator on Unix. If that fails, ensure that all required libraries are installed (you often need to install the “-dev” or “-devel” package for a library, as “base” library packages usually only include the library, and not the headers).

cmake is a “meta” build system; it builds the files (makefiles or IDE project files) used by the “actual” build system (either a “make” program or an IDE). If you’re planning on using an IDE to view, edit, debug or run the code, run “cmake --help” and check the list of generators at the end of the output to see whether the IDE is supported by your version of cmake. If it is, use the name of the IDE (copy and paste the name from the terminal, and put quotes around it) as the argument to -G to get it to build a project file for the IDE instead of building makefiles.

Thank you for that concise breakdown. I ran cmake -G "Unix Makefiles and it seems to have partially worked because it created a new directory called CMakeFiles/ and a file called CMakeCache.txt. But, I also got this error message:

CMake Error at /usr/local/share/cmake-3.15/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
Could NOT find OpenGL (missing: OPENGL_opengl_LIBRARY OPENGL_glx_LIBRARY
Call Stack (most recent call first):
/usr/local/share/cmake-3.15/Modules/FindPackageHandleStandardArgs.cmake:378 (_FPHSA_FAILURE_MESSAGE)
/usr/local/share/cmake-3.15/Modules/FindOpenGL.cmake:397 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
CMakeLists.txt:21 (find_package)

So it looks like I am missing some necessary libraries/resources.

Typically when I see this type of error, I have the needed library package(s) installed, but I haven’t yet installed the header (devel) packages for those libraries.

Search in your package manager for packages starting with “mesa” or “opengl” and install the “-devel” packages for them. That’ll probably resolve it.

Good to know, thank you. In general, do packages with a “-devel” suffix or substring in the name contain the headers corresponding to the libraries contained in the original package?


That’s the usual convention. The web page for the package usually has an option to show the files which the package contains. You’re looking for something containing gl.h (and probably glx.h and glext.h).

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.