Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I cannot link the Mac framework file with CMake?

Tags:

cmake

I have a question related to CMake in MAC. I make sure that the executable program will link the framework and libraries correctly with the following codes:

link_directories(directory_to_framework_and_libs)
add_executable(program ${FILE_LIST})
target_link_libraries(program framework_name lib1 lib2)

In the first line code, I denote the location where the executable program can search for the framework and libraries. In the third line code, the framework and the libraries will link to the executable program. However, when I compile the xcode.project created from the cmake file with Xcode 4, the project keeps complaining that it cannot find -lframework_name: ld: library not found -lframework_name Any ideas will be appreciated.

like image 930
feelfree Avatar asked Jun 12 '13 15:06

feelfree


4 Answers

You can't link to a framework this way, you have to use find_library as it includes some special handling for frameworks on OSX.

Also, don't use link_directories, CMake use full paths to libraries and it's not needed.

Here's some simple example with AudioUnit:

find_library(AUDIO_UNIT AudioUnit)
if (NOT AUDIO_UNIT)
    message(FATAL_ERROR "AudioUnit not found")
endif()

add_executable(program ${program_SOURCES})
target_link_libraries(program ${AUDIO_UNIT})
like image 181
Guillaume Avatar answered Sep 19 '22 13:09

Guillaume


Another solution is as follows:

target_link_libraries(program "-framework CoreFoundation")
target_link_libraries(program "-framework your_frame_work_name")
set_target_properties(program PROPERTIES LINK_FLAGS "-Wl,-F/Library/Frameworks")
like image 39
feelfree Avatar answered Sep 16 '22 13:09

feelfree


You do not need all this hassle (at least with cmake 2.8.12).

This works fine:

target_link_libraries(program stdc++ "-framework Foundation" "-framework Cocoa" objc)

When CMake sees a link parameter starting with "-", it does not prepend "-l" and passes the argument as-is to the linker (/usr/bin/c++).

You need the quotes for frameworks so that CMake treats the two words as a single entry and does not add "-l" before "Foundation" for example.

like image 24
Anna B Avatar answered Sep 20 '22 13:09

Anna B


For cmake version 3.20.1

https://github.com/Sunbreak/cli-breakpad.trial/blob/1800b187afd5f0c29368196561ddb6b55123d4a0/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})
like image 25
Sunbreak Avatar answered Sep 18 '22 13:09

Sunbreak