简体   繁体   English

CMake add_subdirectory不调用子CMakeLists.txt

[英]CMake add_subdirectory not invoking the child CMakeLists.txt

I have a project structure like this which I wish to build using CMake. 我有一个这样的项目结构,我希望使用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

The crystal sub-project is supposed to produce a static library ( libcrystal.a ) in the lib/ folder (using the contents of include/ and src/ ) and the root project will produce an executable out of game/main.cpp linking the libcrystal.a static library. Crystal子项目应该在lib/文件夹中生成一个静态库( libcrystal.a )(使用include/src/的内容),而项目将在game/main.cpp生成一个可执行文件,链接该文件。 libcrystal.alibcrystal.a静态库。

The Parent CMAKE is as follows: 父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)

The child CMAKE is as follows: 子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})

What isn't working: 什么不起作用:

When I executed cmake . 当我执行cmake . and sudo make in the root/ directory I expected both parent and child cmake to run sequentially. sudo makeroot/目录中,我希望父级和子级cmake都可以顺序运行。 But it seems like the child cmake is not getting invoked and thus not producing the .a file. 但是似乎子cmake 没有被调用 ,因此没有产生.a文件。 Its showing some error like: 它显示出一些错误,例如:

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

What is working: 工作原理:

I went ahead the did this 我继续这样做了

  1. executed cmake . 执行cmake . in root/ root/
  2. navigate into the crystal folder and manually invoked the Makefile by sudo make 导航到crystal文件夹并通过sudo make手动调用Makefile
  3. came to the root/ again and invoked the outer Makefile by sudo make 再次进入root/并通过sudo make调用外部Makefile

and this perfectly worked . 完美地工作了

Question: 题:

Why the child CMake is not getting invoked as I mentioned in the What isn't working section ??? 正如我在“ 什么不起作用”部分中提到的那样,为什么未调用子级CMake ???

Use target_link_libraries(thegame ${CRYSTAL_LIB_NAME}) in the root CMakeLists.txt and remove link_directories call. 在根CMakeLists.txt中使用target_link_libraries(thegame ${CRYSTAL_LIB_NAME})并删除link_directories调用。 CMake will recognize that you are linking to crystal target and set makefile dependencies and compiler flags accordingly. CMake将识别出您正在链接到crystal目标并相应地设置makefile依赖项和编译器标志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM