Ezra Goss
Ezra Goss

Reputation: 124

CMake finds target and component but can't link against them

Context

I am trying to build and run this project: https://github.com/tay10r/libpx

I am running CMake 3.19.1 on MacOS 11 and the only thing I've changed from the project source that you can clone is the target OS for editor/BuildDesktop.cmake: I changed WIN32 to MACOSX_BUNDLE. In any event the issue below exists regardless of this change.

Problem

I am trying to build using CMake with a project specific argument. When I do I get the following contradictory error that says OpenGL has been found with the required component and -- simultaneously -- that the target OpenGL::OpenGL can't be found.

$ cmake .. -DLIBPX_EDITOR=ON                                         │
-- GLM: Version 0.9.9.9                                                                              │
-- Using the single-header code from /Users/ezragoss/Projects/libpx/editor/third-party/json/single_in│
clude/                                                                                               │
-- Using Cocoa for window creation                                                                   │
-- Found OpenGL: /Library/Developer/CommandLineTools/SDKs/MacOSX11.0.sdk/System/Library/Frameworks/Op│
enGL.framework  found components: OpenGL                                                             │
-- Configuring done                                                                                  │
CMake Error at editor/BuildDesktop.cmake:6 (add_executable):                                         │
  Target "pxedit_desktop" links to target "OpenGL::OpenGL" but the target was                        │
  not found.  Perhaps a find_package() call is missing for an IMPORTED                               │
  target, or an ALIAS target is missing?                                                             │
Call Stack (most recent call first):                                                                 │
  editor/CMakeLists.txt:85 (include)                                                                 │
                                                                                                     │
                                                                                                     │
-- Generating done                                                                                   │
CMake Generate step failed.  Build files cannot be regenerated correctly.

The .cmake file is in the path I mentioned earlier, editor/BuildDesktop.cmake:

cmake_minimum_required(VERSION 3.8.2)

find_package(OpenGL REQUIRED COMPONENTS OpenGL)  // <-------- relevant line
find_package(GLEW   REQUIRED)

add_executable(pxedit_desktop MACOSX_BUNDLE
  AppStorageDesktop.cpp
  GlRenderer.hpp
  GlRenderer.cpp
  GlslDesktopShaders.hpp
  GlslDesktopShaders.cpp
  GlfwPlatform.cpp
  LocalStorageDesktop.cpp
  $<TARGET_OBJECTS:pxedit_core>)

set_target_properties(pxedit_desktop
  PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")

target_link_libraries(pxedit_desktop
  PRIVATE
    px pxedit_core
    glfw
    imgui_gl
    OpenGL::OpenGL  // <----------- relevant line
    GLEW::GLEW
    sago::platform_folders)

target_compile_options(pxedit_desktop PRIVATE ${px_cxxflags})

target_compile_definitions(pxedit_desktop PRIVATE PXEDIT_DESKTOP=1)

target_compile_features(pxedit_desktop PRIVATE cxx_std_17)

Question

What am I missing here? I don't know enough about cmake's internals to know whether it tries to link against a separate path than the one it finds via find_package but that seems like it would be unintuitive.

Upvotes: 1

Views: 1216

Answers (1)

Alex Reinking
Alex Reinking

Reputation: 19946

According to the documentation, find_package(OpenGL) defines the target OpenGL::OpenGL only if your system is GLVND-based. But GLVND is the Linux-specific vendor-neutral dispatch library, so there's no way it would be present on your macOS system. So:

  1. find_package(OpenGL REQUIRED COMPONENTS OpenGL) will consider the "OpenGL" feature to be found if either OpenGL::GL or OpenGL::OpenGL is available.
  2. The author of the application you're trying to build will have to adjust their build files for macOS. Most likely, the best thing to do is bump the minimum required CMake version to at least 3.10 and use the OpenGL::GL target always. Otherwise, they will need to set OpenGL_GL_PREFERENCE to GLVND.

For now, you should try replacing OpenGL::OpenGL with OpenGL::GL and see if the build can proceed.

Upvotes: 2

Related Questions