8

For Macos, I'd like to link to some framework. In windows, I would like to link to some library.

For example, OpenGL Framework, how to express this requirement using cmake?

Adam Lee
  • 24,710
  • 51
  • 156
  • 236

4 Answers4

11

You could try the following code:

target_link_libraries(<target name>
    "-framework AVFoundation"
    "-framework CoreGraphics"
    "-framework CoreMotion"
    "-framework Foundation"
    "-framework MediaPlayer"
    "-framework OpenGLES"
    "-framework QuartzCore"
    "-framework UIKit"
    )
2

To tell CMake that you want to link to OpenGL, add the following to your CMakeLists.txt:

find_package(OpenGL REQUIRED)
include_directories(${OPENGL_INCLUDE_DIR})
target_link_libraries(<your program name> ${OPENGL_LIBRARIES})

find_package will look for OpenGL and tell the rest of the script where OpenGL is by setting some OPENGL* variables. include_directories tells your compiler where to find OpenGL headers. target_link_libraries instructs CMake to link in OpenGL.

The following code will do different actions based on the operating system:

if(WIN32)
    #Windows specific code
elseif(APPLE)
    #OSX specific code
endif()
Quip Yowert
  • 444
  • 2
  • 7
1

You could try the following macro code:

macro(ADD_OSX_FRAMEWORK fwname target)
    find_library(FRAMEWORK_${fwname}
    NAMES ${fwname}
    PATHS ${CMAKE_OSX_SYSROOT}/System/Library
    PATH_SUFFIXES Frameworks
    NO_DEFAULT_PATH)
    if( ${FRAMEWORK_${fwname}} STREQUAL FRAMEWORK_${fwname}-NOTFOUND)
        MESSAGE(ERROR ": Framework ${fwname} not found")
    else()
        TARGET_LINK_LIBRARIES(${target} PUBLIC "${FRAMEWORK_${fwname}}/${fwname}")
        MESSAGE(STATUS "Framework ${fwname} found at ${FRAMEWORK_${fwname}}")
    endif()
endmacro(ADD_OSX_FRAMEWORK)

Example

ADD_OSX_FRAMEWORK(Foundation ${YOUR_TARGET}) # Add the foundation OSX Framework

You can find this example code here

fenix688
  • 2,435
  • 2
  • 17
  • 23
1

For custom framework

cmake version 3.20.1

https://github.com/Sunbreak/cli-breakpad.trial/blob/master/CMakeLists.txt#L10-L12

if(APPLE)
    find_library(BREAKPAD_CLIENT Breakpad "${CMAKE_CURRENT_SOURCE_DIR}/breakpad/mac/")
    target_link_libraries(cli-breakpad PRIVATE ${BREAKPAD_CLIENT})
Sunbreak
  • 391
  • 3
  • 8