zhangxaochen
zhangxaochen

Reputation: 34027

cmake find_package(OpenGL REQUIRED) failed without any error message

I'm trying to use cmake (Windows10 + VS2010) to find OpenGL and I used the following code:

find_package(OpenGL REQUIRED)
message(STATUS "OpenGL_FOUND:= ${OpenGL_FOUND}")

if(OpenGL_FOUND)
    message('OpenGL_FOUND-is-true')
else(OpenGL_FOUND)
    message('OpenGL_FOUND-is-false')
endif(OpenGL_FOUND)

message(STATUS "OPENGL_INCLUDE_DIR:= ${OPENGL_INCLUDE_DIR}")
message(STATUS "OPENGL_LIBRARY:= ${OPENGL_LIBRARY}")
message(STATUS "OPENGL_LIBRARIES:= ${OPENGL_LIBRARIES}")

The output is:

OpenGL_FOUND:= 
'OpenGL_FOUND-is-false'
OPENGL_INCLUDE_DIR:= 
OPENGL_LIBRARY:= glu32;opengl32
OPENGL_LIBRARIES:= glu32;opengl32

the arg REQUIRED in find_package does NOT take effect, why is that?

Upvotes: 1

Views: 3517

Answers (2)

Andreas Haferburg
Andreas Haferburg

Reputation: 5520

The real answer is this part of the https://cmake.org/cmake/help/latest/command/find_package.html:

In Module mode the loaded find module is responsible to honor the request detailed by these variables; see the find module for details.

So you could argue that this is a bug in the FindOpenGL.cmake module. Alternatively, the requirement by find_package that each and every module is supposed to handle the same thing can also be considered a bug. Or that they don't have any process in place to only ship modules that satisfy these requirements.

Upvotes: 0

rocambille
rocambille

Reputation: 15986

Looking in the documentation, as unexpected, the variable is not OpenGL_FOUND, but OPENGL_FOUND. I guess you will have something more consistent with the following:

find_package(OpenGL REQUIRED)
message(STATUS "OPENGL_FOUND:= ${OPENGL_FOUND}")

if(OPENGL_FOUND)
    message('OPENGL_FOUND-is-true')
else()
    message('OPENGL_FOUND-is-false')
endif()

message(STATUS "OPENGL_INCLUDE_DIR:= ${OPENGL_INCLUDE_DIR}")
message(STATUS "OPENGL_LIBRARY:= ${OPENGL_LIBRARY}")
message(STATUS "OPENGL_LIBRARIES:= ${OPENGL_LIBRARIES}")

add_executable(foo main.cpp)
target_include_directories(foo PUBLIC ${OPENGL_INCLUDE_DIR})
target_link_libraries(foo ${OPENGL_LIBRARIES})

Looking in the source code, OPENGL_INCLUDE_DIR is not set on Windows, except for cygwin, and should indeed be empty in your case.

Note that repeating the condition is optional in else() and endif().

Upvotes: 3

Related Questions