Quincy
Quincy

Reputation: 1949

How to add a framework to CMake

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.

Upvotes: 23

Views: 30043

Answers (3)

Cameron Lowell Palmer
Cameron Lowell Palmer

Reputation: 22245

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 )

Upvotes: 0

andrewchan2022
andrewchan2022

Reputation: 5310

another solution: https://stackoverflow.com/a/18330634/2482283

target_link_libraries(program "-framework Cocoa")

Upvotes: 15

choobablue
choobablue

Reputation: 752

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.

Upvotes: 32

Related Questions