CMake link_directories从库 [英] CMake link_directories from library
问题描述
我试图使用CMake和Xcode从另一个库链接到库。
这是任何库的问题,但为了使事情更容易传达,让我们使用 zlib
为例。
这似乎适用于可执行文件,如下所示:
LINK_DIRECTORIES($ {LIB_DIR} / zlib / build /
ADD_EXECUTABLE(MY_EXECUTABLE ...
并且生成一个Xcode项目, :
>
可以看到, $(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)
位被正确添加到 zlib
库路径。
但如果我尝试这样做:
LINK_DIRECTORIES($ {LIB_DIR} / zlib / build /)
ADD_LIBRARY(MY_LIBRARY ...
$当我链接到<$ c时,b
$ b
zlib
永远不会链接到 MY_EXECUTABLE
$ c> MY_LIBRARY
和 TARGET_LINK_LIBRARIES
之后 ADD_LIBRARY
允许我从 MY_LIBRARY
链接到 zlib
,但是我必须指定完整路径将不会工作作为配置(调试,释放等)以及有效的平台(iphoneos,iphonesimulator等)是因素。
我想做什么是使用 $(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)
位将 zlib
添加到Xcode库搜索路径,如上所示。
将我的评论转换为答案
CMake支持使用生成器表达式(参见例如 CMake - 访问多配置生成器的配置参数) / p>
和参数 target_link_libraries()支持使用生成器表达式。所以在你的情况下,你可以使用 $< CONFIG>
生成器表达式看起来像这样:
TARGET_LINK_LIBRARIES(MY_LIBRARY $ {LIB_DIR} / zlib / build / $< CONFIG> / ...)
请注意 - target_link_libraries()
有关政策的文档注意 - 如果您可能更改了一些政策=http://www.cmake.org/cmake/help/v3.3/policy/CMP0003.html#policy:CMP0003 =nofollow> CMP0003 - 通过完整路径链接的库不再生成链接器搜索路径和 CMP0004 - 已关联的库可能没有前导或尾随空格:
请注意,生成器表达式不会用于OLD
处理CMP0003或CMP0004
I'm trying to link to a library from another library using CMake and Xcode.
This is an issue for any library, but to make things easier to convey, let's use zlib
as an example.
This seems to work for executables as follows:
LINK_DIRECTORIES(${LIB_DIR}/zlib/build/)
ADD_EXECUTABLE(MY_EXECUTABLE ...
And it generates an Xcode project with the setting shown below:
As you can see, the $(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)
bit gets added correctly, to the zlib
library path.
But if I try to do this:
LINK_DIRECTORIES(${LIB_DIR}/zlib/build/)
ADD_LIBRARY(MY_LIBRARY ...
zlib
never gets linked to MY_EXECUTABLE
when I link it to MY_LIBRARY
And TARGET_LINK_LIBRARIES
after ADD_LIBRARY
allows me to link to zlib
from MY_LIBRARY
but I have to specify the full path, which won't work as the configuration (Debug, Release, etc) as well as the effective platform (iphoneos, iphonesimulator, etc) are factors.
What I want to do is to have zlib
added to the Xcode Library Search Paths, with the $(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)
bit, as shown above.
Turning my comment into an answer
CMake does support adding the appropriate configuration to paths in multi-configuration environments with generator expressions (see e.g. CMake - Accessing configuration parameters of multiple-configuration generators)
And arguments to target_link_libraries() support the use of generator expressions. So in your case you can make use of the $<CONFIG>
generator expression which would look something like this:
TARGET_LINK_LIBRARIES(MY_LIBRARY ${LIB_DIR}/zlib/build/$<CONFIG>/...)
Be aware - if you may have changed some policies - of one note from the target_link_libraries()
documentation about policies CMP0003 - Libraries linked via full path no longer produce linker search paths and CMP0004 - Libraries linked may not have leading or trailing whitespace:
Note however, that generator expressions will not be used in OLD handling of CMP0003 or CMP0004
这篇关于CMake link_directories从库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!