cmake 生成的 Xcode 项目 - 发布构建有效,但归档因链接器错误而失败
使用 Xcode 6.3.1,CMake 3.2.2
Using Xcode 6.3.1, CMake 3.2.2
我有一个与图书馆链接的项目.这个库作为代码包含在 xcode-project 中,编译后与主可执行文件链接.
I have a project which links with a library. This library is included in the xcode-project as code, compiled and then linked with the main executable.
项目是用cmake生成的.CMakeLists.txt 的一些摘录:
The project is generated with cmake. Some extracts of the CMakeLists.txt:
add_library(mylib ${mylib_HEADERS} pch.cpp source/mylib/xxx.cpp)
...
add_executable(${MAIN_BINARY_NAME} MACOSX_BUNDLE ${MAIN_HEADERS} ${MAIN_CODE_FILES} ${MAIN_ICON_FILES} ${MAIN_DYLIBS} )
target_link_libraries (${MAIN_BINARY_NAME} mylib)
生成我的 xcodeproj 后,我构建了一个正常版本 ( cmd + B ),它可以毫无问题地编译和链接(和运行).但是,当我尝试存档时,它因链接器错误而失败.
After generating my xcodeproj, I build a normal version ( cmd + B ) which compiles and links (and runs) without issues. When I try to archive however it fails on a linker-error.
使用命令行 xcodebuild 我比较了两个版本,一些摘录:
Using commandline xcodebuild I compared both versions, some extracts:
发布构建
Libtool /Users/username/dev/MyProject/cmake-master/libs/mylib/RelWithDebInfo/libmylib.a normal x86_64
归档构建
Libtool /Users/username/Library/Developer/Xcode/DerivedData/MyProject-facomnlcdbuduqeohionewjyectq/ArchiveIntermediates/MyProject/IntermediateBuildFilesPath/UninstalledProducts/libmylib.a normal x86_64
...
Ld /Users/username/Library/Developer/Xcode/DerivedData/MyProject-facomnlcdbuduqeohionewjyectq/ArchiveIntermediates/MyProject/InstallationBuildProductsLocation/Applications/MyProject.app/Contents/MacOS/MyProject normal x86_64
...
clang: error: no such file or directory: '/Users/username/dev/myproject/cmake-master/libs/mylib/RelWithDebInfo/libmylib.a'
所以对于release-builds,它正确地使用了cmake指定的构建路径.对于存档构建,它忽略构建路径,而是编译并将生成的库放在默认中间文件夹中 - 但是当与 exe 链接时,它会再次查看 cmake 指定的构建路径,然后失败找到图书馆.
So for release-builds, it correctly uses the build path specified by cmake. For archive-builds, it ignores the build-path and instead compiles and puts the resulting library in the default-intermediate-folder - but then when linking with the exe it does again look in the cmake-specified build-path and then fails to find the library.
它看起来像是 xcode 中的一个错误,它出现是因为 cmake 覆盖了构建路径......?
It looks like a bug in xcode, which turns up because cmake overrides the build-path... ?
同时我找到了一个解决方法,所以至少它没有链接器错误.在 cmakelists.txt 中指定每个配置构建路径",如下所示:
In the meanwhile I found a work-around, so at least it Archives without linker errors. Specify a "per configuration build path" in the cmakelists.txt like this:
set_target_properties(mylib PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/out/library)
存档将编译库 - 稍后在链接时找到它
And archive will compile the library - and find it later, when linking