<div dir="ltr">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:<div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><font face="monospace, monospace">target_link_libraries(foo PRIVATE -framework AppKit)</font></div></blockquote><div><br></div><div>This fails because the link command ends up with <font face="monospace, monospace">-framework -lAppKit</font> instead of <font face="monospace, monospace">-framework AppKit</font>. Maybe that's expected, but then the following DOES work:</div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><span style="font-family:monospace,monospace">target_link_libraries(foo PRIVATE "-framework AppKit")</span></div></blockquote><div><br clear="all"><div>Strangely, with the library quoted as above, the embedded space is not escaped, leading to the (desirable but surprising) result <span style="font-family:monospace,monospace">-framework AppKit</span> in the linker command line (i.e. the space isn't escaped and the two words are not quoted). <b>Which of these is the correct way (if either) and are both behaving as expected?</b></div><div><br></div><div>As extra context, a correct final linker command line can also be achieved like this:</div><div><br></div></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div><font face="monospace, monospace">find_library(APPKIT_LIB AppKit)</font></div></div><div><div><div><span style="font-family:monospace,monospace">target_link_libraries(foo PRIVATE ${APPKIT_LIB})</span></div></div></div></blockquote><div><div><span style="font-family:monospace,monospace"><br></span></div><div>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 <span style="font-family:monospace,monospace">-framework AppKit</span>. This is mentioned in the find_library() docs:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><span style="color:rgb(68,68,68);font-family:sans-serif;font-size:16px;text-align:justify">If the library found is a framework, then </span><code class="gmail-docutils gmail-literal" style="background-color:rgb(236,240,243);padding:0px 1px;font-size:0.95em;color:rgb(68,68,68);text-align:justify"><span class="gmail-pre"><VAR></span></code><span style="color:rgb(68,68,68);font-family:sans-serif;font-size:16px;text-align:justify"> will be set to the full path to the framework </span><code class="gmail-docutils gmail-literal" style="background-color:rgb(236,240,243);padding:0px 1px;font-size:0.95em;color:rgb(68,68,68);text-align:justify"><span class="gmail-pre"><fullPath>/A.framework</span></code><span style="color:rgb(68,68,68);font-family:sans-serif;font-size:16px;text-align:justify">. When a full path to a framework is used as a library, CMake will use a </span><code class="gmail-docutils gmail-literal" style="background-color:rgb(236,240,243);padding:0px 1px;font-size:0.95em;color:rgb(68,68,68);text-align:justify"><span class="gmail-pre">-framework</span> <span class="gmail-pre">A</span></code><span style="color:rgb(68,68,68);font-family:sans-serif;font-size:16px;text-align:justify">, and a </span><code class="gmail-docutils gmail-literal" style="background-color:rgb(236,240,243);padding:0px 1px;font-size:0.95em;color:rgb(68,68,68);text-align:justify"><span class="gmail-pre">-F<fullPath></span></code><span style="color:rgb(68,68,68);font-family:sans-serif;font-size:16px;text-align:justify"> to link the framework to the target.</span><br></blockquote><div><br></div><div>So it is clear why the find_library() approach works, but the behaviour of directly specifying the framework without find_library() seems somewhat surprising.</div><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr">Craig Scott<br><div>Melbourne, Australia</div><div><a href="https://crascit.com" target="_blank">https://crascit.com</a><br></div></div></div></div></div></div></div>
</div></div>