how to use framework using Cmake?

2019-04-13 09:29发布

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?

3条回答
啃猪蹄的小仙女
2楼-- · 2019-04-13 09:36

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"
    )
查看更多
够拽才男人
3楼-- · 2019-04-13 09:41

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

查看更多
Bombasti
4楼-- · 2019-04-13 09:43

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()
查看更多
登录 后发表回答