![](/img/trans.png)
[英]CMake imported targets in add_subdirectory not available in main CMakeLists.txt
[英]CMake add_subdirectory not invoking the child CMakeLists.txt
我有一个这样的项目结构,我希望使用CMake构建。
root\
|
|--crystal\
| |
| |--include\ Math.h, Window.h
| |--src\ Math.cpp, Window.cpp
| |--lib\
| |--CMakeLists.txt // the CHILD cmake
|
|--game\ main.cpp
|--CMakeLists.txt // the PARENT cmake
该Crystal子项目应该在lib/
文件夹中生成一个静态库( libcrystal.a
)(使用include/
和src/
的内容),而根项目将在game/main.cpp
生成一个可执行文件,链接该文件。 libcrystal.a
。 libcrystal.a
静态库。
父CMAKE如下:
cmake_minimum_required(VERSION 2.8.1)
project(thegame)
set(CRYSTAL_LIB_DIR lib)
set(CRYSTAL_LIB_NAME crystal)
add_subdirectory(${CRYSTAL_LIB_NAME})
set(LINK_DIR ${CRYSTAL_LIB_NAME}/${CRYSTAL_LIB_DIR})
set(SRCS game/main.cpp)
link_directories(${LINK_DIR})
include_directories(${CRYSTAL_LIB_NAME}/include)
add_executable(thegame ${SRCS})
target_link_libraries(thegame lib${CRYSTAL_LIB_NAME}.a)
子CMAKE如下:
cmake_minimum_required(VERSION 2.8.1)
project(crystal)
include_directories( include )
file(GLOB_RECURSE SRC "src/*.cpp")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CRYSTAL_LIB_DIR})
add_library(${CRYSTAL_LIB_NAME} STATIC ${SRC})
什么不起作用:
当我执行cmake .
和sudo make
在root/
目录中,我希望父级和子级cmake都可以顺序运行。 但是似乎子cmake 没有被调用 ,因此没有产生.a
文件。 它显示出一些错误,例如:
Scanning dependencies of target thegame
[ 20%] Building CXX object CMakeFiles/thegame.dir/game/main.cpp.o
[ 40%] Linking CXX executable thegame
/usr/bin/ld: cannot find -lcrystal
collect2: error: ld returned 1 exit status
CMakeFiles/thegame.dir/build.make:94: recipe for target 'thegame' failed
make[2]: *** [thegame] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/thegame.dir/all' failed
make[1]: *** [CMakeFiles/thegame.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
工作原理:
我继续这样做了
cmake .
root/
crystal
文件夹并通过sudo make
手动调用Makefile root/
并通过sudo make
调用外部Makefile 这完美地工作了 。
题:
正如我在“ 什么不起作用”部分中提到的那样,为什么未调用子级CMake ???
在根CMakeLists.txt中使用target_link_libraries(thegame ${CRYSTAL_LIB_NAME})
并删除link_directories
调用。 CMake将识别出您正在链接到crystal
目标并相应地设置makefile依赖项和编译器标志。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.