CMake link_directories from library

2019-03-04 03:34发布

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:

enter image description here

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.

标签: xcode cmake
1条回答
兄弟一词,经得起流年.
2楼-- · 2019-03-04 04:37

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

查看更多
登录 后发表回答