21

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.

1
  • 2
    It's hard to say what's wrong here without more specific details. In principal, it appears correct. However, I'd recommend avoiding the use of link_directories (see the docs for more info) and use find_library instead. This will allow you to pass the full path to the library in the target_link_libraries call, and if the library hasn't been found, you'll get an error at CMake configure time rather than at link time.
    – Fraser
    Commented Jun 12, 2013 at 19:19

7 Answers 7

35

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")
1
  • This is the one that works for me for non-system, custom path frameworks
    – avee
    Commented Aug 21, 2020 at 0:18
33

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})
2
  • 1
    Your if not(...) should be if(NOT ...) I think.
    – Fraser
    Commented Jun 12, 2013 at 20:41
  • Still not working with CMake 2.8.12, if program is actually static library target. In Build Settings Xcode pane, Linker flags remain empty, framework search paths remain empty, in Build Phases pane, no dependency is added, no linkage is added. I observed that in empty project, it is sufficient ot define linker flags and search paths.
    – lef
    Commented Mar 25, 2014 at 23:03
18

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.

1
  • This is much more in line with standard Apple practice, and since the Apple frameworks are all OS specific anyway (and thus going to be conditionalized for the platform), the more "general" approach mentioned above has no advantages. Commented Jun 11, 2021 at 4:54
1

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})
1

I wrote this using @Guillaume s comment:

if (APPLE)
    find_library(COCOA Cocoa)
    if (NOT COCOA)
        message(FATAL_ERROR "Cocoa not found")
    endif()
    target_link_libraries(yourtarget PRIVATE ${COCOA})
endif()

This works best for me because it does not get run on other platforms and it can even be used when the target is a library (add_library). You can replace the Cocoa framework by any one you want ofc.

0

For recent versions of CMake,

target_link_libraries(program PRIVATE "-framework framework_name")
0

CMake needs to see the full path to the framework to recognize it as such and compose appropriate command line arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.