【发布时间】:2016-12-25 07:42:06
【问题描述】:
我有一个像这样的项目结构,我希望使用 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
水晶子项目应该在lib/文件夹中生成一个静态库(libcrystal.a)(使用include/和src/的内容)和root 项目将从game/main.cpp 链接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})
什么不起作用:
当我在 root/ 目录中执行 cmake . 和 sudo make 时,我希望父 cmake 和子 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
什么是有效的:
我继续做了这个
- 在
root/中执行cmake . - 导航到
crystal文件夹并通过sudo make手动调用Makefile - 再次来到
root/并通过sudo make调用外部Makefile
而且这完美运行。
问题:
为什么没有像我在 什么不起作用 部分中提到的那样调用子 CMake ???
【问题讨论】:
-
一般建议:不要使用 CMake 进行 in-source 构建(即
cmake .)。在项目目录树之外创建一个单独的构建目录(例如,作为root/的兄弟)。