c++windowsopenglcmakeglu

CMake on windows - bug in FindOpenGL.cmake when using imported target OpenGL::GLU?


So I'm trying to use cmake on windows to build an OpenGL application, and I'm having problems linking to GLU.

Specifically, I'm using cmake 3.11's FindOpenGL.cmake to try to bring in OpenGL::GLU as an imported target, since I want to use "modern" cmake best practices. My CMakeLists.txt has this section:

find_package(OpenGL REQUIRED)

if(NOT TARGET OpenGL::GLU)
    message(FATAL_ERROR "GLU could not be found")
endif(NOT TARGET OpenGL::GLU)

message(STATUS "glu: ${OPENGL_glu_LIBRARY}")

get_target_property(GLU_IMPORTED_LIBNAME OpenGL::GLU IMPORTED_LIBNAME)
message(STATUS "glu imported libname: ${GLU_IMPORTED_LIBNAME}")

...and then near the end:

target_link_libraries(OpenGLTest PRIVATE
    ${OS_LIBRARIES}
    OpenGL::GL
    OpenGL:GLU
    GLUT::GLUT
    )

It seems to find GLU correctly - passes my if check for TARGET OpenGL::GLU, and my STATUS printouts are:

 -- glu: glu32
 -- glu imported libname: glu32

Having the libname as "just" glu32 (no .lib, no pathname) seems a bit odd, but maybe that's fine...? The weird bit is then when I try to build / link, I see this error:

C:\...\LINK : fatal error LNK1104: cannot open file 'OpenGL:GLU.lib'

Taking a look at the generated build.ninja (I'm using the Ninja generator), I see this:

build OpenGLTest.exe: ...
  LINK_LIBRARIES = opengl32.lib OpenGL:GLU.lib C:\...\freeglut.lib

So... the OpenGL::GL and GLUT::GLUT get properly translated into real pathnames, but somehow, the OpenGL::GLU target gets translated into OpenGL:GLU.lib, which is clearly wrong. This seems like a bug in either the way FindOpenGL.cmake is set up, or a bug in cmake itself (which seems less likely).

Has anyone else ever experienced something similar, or have any insight into what might be going on here? Should I file a bug report against cmake?


Solution

  • There's a typo in your CMake script - it should be OpenGL::GLU, not OpenGL:GLU:

    target_link_libraries(OpenGLTest PRIVATE 
        ${OS_LIBRARIES} OpenGL::GL
        OpenGL:GLU # single colon!
        GLUT::GLUT
    )