cmake generated Xcode-project - release-build works but archive fails on linker errors

旧街凉风 提交于 2019-12-23 08:36:51

问题


Using Xcode 6.3.1, CMake 3.2.2

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.

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)

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.

Using commandline xcodebuild I compared both versions, some extracts:

release-build

Libtool /Users/username/dev/MyProject/cmake-master/libs/mylib/RelWithDebInfo/libmylib.a normal x86_64

archive-build

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'

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.

It looks like a bug in xcode, which turns up because cmake overrides the build-path... ?


回答1:


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



来源:https://stackoverflow.com/questions/30318941/cmake-generated-xcode-project-release-build-works-but-archive-fails-on-linker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!