Cmake doesn't find gl.h on OS X

I am on OS X 10.10 and am trying to build a C project with GLUT and OpenGL. I have led it to a minimal example demonstrating my problem. I have the following CMakeLists.txt

:

cmake_minimum_required(VERSION 2.8)
FIND_PACKAGE(OpenGL REQUIRED)
FIND_PACKAGE(GLUT REQUIRED)

if(OpenGL_FOUND) # never true, but printed as true
    link_directories(${OpenGL_LIBRARY_DIRS})
    include_directories(${OpenGL_INCLUDE_DIR})
endif(OpenGL_FOUND)

if(GLUT_FOUND)
    link_directories(${GLUT_LIBRARY_DIR})
    include_directories(${GLUT_INCLUDE_DIR})
endif(GLUT_FOUND)

# print all vars because wtf
get_cmake_property(_v VARIABLES)
foreach(_v ${_v})
   message(STATUS "${_v}=${${_v}}")
endforeach()


add_executable(main main.c)

target_link_libraries(main ${GLUT_LIBRARY} ${OPENGL_LIBRARY})

      

main.c

is just a dummy containing two titles:

#include <gl.h>
#include <glut.h>
int main()
{    
   return 0;
}

      

cmake .

Works fine now and prints all variables for debugging purposes. I took the code from somewhere, I don't know enough about cmake to see if it does what I think it does. Anyway, the launch make

returns

main.c:1:10: fatal error: 'gl.h' file not found
#include <gl.h>
         ^
1 error generated.

      

The header is gl.h

actually present in /System/Library/Frameworks/OpenGL.framework/Headers

and as such should be found by cmake, especially as it glut.h

is in the same structure (just replace OpenGL with GLUT) and found just fine. Also, what confuses me is that the block in is if(GLUT_FOUND)...

never executed (try putting a statement in it message

), but among the printable variables, it says OPENGL_FOUND=TRUE

. But removing the if condition doesn't change anything.

Hot question: What the hell is going on? Why does a) cmake not find the header if it is not included in it, b) if-block is not executed even though it OPENGL_FOUND

prints as TRUE, c) such problems do not occur with glut.h

? Spent hours on this and can't figure out why.

+3


source to share


4 answers


As pointed out bei pmr, CMake variables are case sensitive, so the variable needs to be queried OPENGL_FOUND

. Also, as PeterT writes, the title is included as #include <OpenGL/gl.h>

in OS X.



0


source


It is customary to do

#if defined(__APPLE__)
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#endif

      



You can see this is done in one form or another in glfw, glew, sfml and others

+8


source


I'm surprised you found the OpenGL headers /System/Library/Frameworks

in OS X 10.10. I don't think they were installed there in quite a few versions of Xcode. The most recent header files from Xcode 6.1 to 10.10 should be in:

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/System/Library/Frameworks/OpenGL.framework/Headers

You really don't need to know this path if you don't want to look at the headlines. I believe the compiler will automatically use the SDK that matches the OS you are compiling. If for some reason you wanted to create a different platform, you can override this logic with a -isysroot

compiler option .

With header files that come from the framework, the name you use in your expression is #include

:

#include <FrameworkName/HeaderFileName.h>

      

The compiler will resolve this actual title path name within the frame.

Therefore, if you want to use the current OpenGL header, which is gl3.h

, from the OpenGL framework, the correct include statement is:

#include <OpenGL/gl3.h>

      

This will give you access to the main profile of the most supported version of OpenGL (which is 3.x or 4.x if you have a fairly new Mac). Or if you want to use OpenGL 2.1 with deprecated features:

#include <OpenGL/gl.h>

      

+2


source


I ended up getting this question after updating the qt installed with homebrew and I got the same error messages. Moving away from Reto's comments, I updated CMAKE_OSX_SYSROOT to /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk

and everything went back to work as expected.

0


source







All Articles