I'm exploring the behaviour of target_link_libraries() where the library to
be linked is an Apple framework. As part of this, I noticed that the
following does not work:

Advertising

target_link_libraries(foo PRIVATE -framework AppKit)
This fails because the link command ends up with -framework -lAppKit
instead of -framework AppKit. Maybe that's expected, but then the following
DOES work:
target_link_libraries(foo PRIVATE "-framework AppKit")
Strangely, with the library quoted as above, the embedded space is not
escaped, leading to the (desirable but surprising) result -framework AppKit in
the linker command line (i.e. the space isn't escaped and the two words are
not quoted). *Which of these is the correct way (if either) and are both
behaving as expected?*
As extra context, a correct final linker command line can also be achieved
like this:
find_library(APPKIT_LIB AppKit)
target_link_libraries(foo PRIVATE ${APPKIT_LIB})
In this case, while APPKIT_LIB contains a full absolute path to the
framework, CMake manages to recognise it as a framework and shortens the
linker flags to just -framework AppKit. This is mentioned in the
find_library() docs:
If the library found is a framework, then <VAR> will be set to the full
> path to the framework <fullPath>/A.framework. When a full path to a
> framework is used as a library, CMake will use a -framework A, and a
> -F<fullPath> to link the framework to the target.
>
So it is clear why the find_library() approach works, but the behaviour of
directly specifying the framework without find_library() seems somewhat
surprising.
--
Craig Scott
Melbourne, Australia
https://crascit.com