How to add a framework to CMake
Asked Answered
T

3

23

I have written a small helloworld program that needs the Cocoa framework. I'd like to know how to add a framework in CMake. All the information I have found is out-of-date. I have CMake 2.8.1 on Snow Leopard.

Trite answered 25/5, 2010 at 21:26 Comment(0)
F
33

Can you just use find_library like this: find_library(COCOA_LIBRARY Cocoa)?

Then use ${COCOA_LIBRARY} in your target_link_libraries. Possibly setting the CMAKE_FIND_FRAMEWORK variable to ONLY.

Also, refer to this article: How to use existing OSX frameworks.

Floe answered 4/6, 2010 at 19:41 Comment(1)
How does this guarantee that the Cocoa framework version of the library is included as opposed to a regular unix style library?Theseus
C
15

another solution: https://mcmap.net/q/584913/-why-i-cannot-link-the-mac-framework-file-with-cmake

target_link_libraries(program "-framework Cocoa")
Charlottecharlottenburg answered 21/12, 2018 at 13:51 Comment(0)
T
0

Create a Macro: target_link_framework

Supports public and private frameworks

macro( target_link_framework TARGET NAME )
    find_library( FRAMEWORK_${NAME}
        NAMES ${NAME}
        PATHS ${CMAKE_OSX_SYSROOT}/System/Library
        PATH_SUFFIXES Frameworks PrivateFrameworks
        CMAKE_FIND_FRAMEWORK only
        NO_DEFAULT_PATH )
    if( ${FRAMEWORK_${NAME}} STREQUAL FRAMEWORK_${NAME}-NOTFOUND)
        message( ERROR ": Framework ${NAME} not found" )
    else()
        target_link_libraries( ${TARGET} PUBLIC "${FRAMEWORK_${NAME}}" )
        message( STATUS "Framework ${NAME} found at ${FRAMEWORK_${NAME}}" )
    endif()
endmacro( target_link_framework )
Terrific answered 17/1 at 12:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.