Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake: opengl include dirs are named differently on different platforms

Cmake founds include directory for opengl, but opengl headers are in a subdirectory that is named differently on different platforms (and, maybe, compilers): gl on Windows, GL on Linux, OpenGL (as far as I know) on Mac. Thus, adding OPENGL_INCLUDE_DIRECTORY to include paths doesn't help very much - I still have to include (or , and so on) in my sources. How should I deal with it?

like image 712
lisyarus Avatar asked Apr 24 '13 13:04

lisyarus


1 Answers

I assume you mean OPENGL_INCLUDE_DIR, not OPENGL_INCLUDE_DIRECTORY.

You have a couple of choices here. The simplest is to add ${OPENGL_INCLUDE_DIR}/GL or ${OPENGL_INCLUDE_DIR}/OpenGL to your include search paths and use

#include "gl.h"

in your source code.

The case of filenames is disregarded on Windows, so you don't need to use /gl and /GL - simply /GL will do.

To achieve this, in your CMakeLists.txt do:

if(APPLE)
  include_directories(${OPENGL_INCLUDE_DIR}/OpenGL)
else()
  include_directories(${OPENGL_INCLUDE_DIR}/GL)
endif()


To make this a bit more robust, you can have CMake find the path to "gl.h" and include that:

find_path(OpenglIncludeSubdir
            NAMES gl.h
            PATHS ${OPENGL_INCLUDE_DIR}
            PATH_SUFFIXES GL OpenGL
            NO_DEFAULT_PATH)
include_directories(${OpenglIncludeSubdir})

Again, in your source you'd use:

#include "gl.h"


Possibly the most robust (i.e. avoiding further include paths beyond ${OPENGL_INCLUDE_DIR}) would be to find the path to "gl.h" and then use configure_file to apply the correct #include term. You would use an input file which contained something like:

#include "@OpenglSubdir@/gl.h"

and then running configure_file would replace @OpenglSubdir@ with its value and write the contents to an output file. This output file would then be included in your target and would have

#include "GL/gl.h"

or

#include "OpenGL/gl.h"

as appropriate.

To achieve this, you'd do something like:

find_file(OpenglSubdir
            NAMES GL OpenGL
            PATHS ${OPENGL_INCLUDE_DIR}
            NO_DEFAULT_PATH)
get_filename_component(OpenglSubdir ${OpenglSubdir} NAME)
configure_file(${CMAKE_SOURCE_DIR}/my_config.h.in ${CMAKE_SOURCE_DIR}/my_config.h)
like image 51
Fraser Avatar answered Oct 18 '22 08:10

Fraser