【发布时间】:2019-10-06 04:33:58
【问题描述】:
我正在尝试使用依赖于第三方库的 cmake 构建程序。这个第三方库包含一个 CMakeLists.txt 文件,所以我想要做的是将第三方库的源代码保留在我的项目目录中,并使用 add_subdirectory(path/to/lib) 构建它,然后将我的目标链接到第三方库生成的静态库。
我的 CMakeLists.txt:
cmake_minimum_version(VERSION 3.10)
project(my_project)
add_subdirectory("${CMAKE_SOURCE_DIR}/extern/somelib")
# my-code:
# somelib CMakeLists.txt file has a project name: SOMELIB
# which lets me access the directory where the files are built
# on windows it builds to /Release, on mac and linux it just builds
# to the binary dir
set(SOMELIB_LIBS "${SOMELIB_BINARY_DIR}/Release")
add_executable(my_program my_main.cpp)
target_link_libraries(my_program "${SOMELIB_LIBS}/SOMELIB.lib" "${SOMELIB_LIBS}/SOMELIBmain.lib")
然后我创建一个构建目录并从该目录开始:
cmake -G "Visual Studio 15 2017" ..
cmake --build .
构建命令失败并显示“LINK : fatal error LNK1181: cannot open input file 'extern/somelib/Release/SOMELIBmain.lib' ...”
我现在的解决方法是注释掉“#my-code”部分,首先构建生成静态库的 somelib 依赖项,然后取消注释掉 my-code 并再次构建,然后才能正常工作。
如何告诉 CMake 先构建子目录,然后链接到它生成的静态库?
【问题讨论】:
标签: c++ cmake linker dependencies