我目前正在使用递归make和autotools,我正在寻找迁移到CMake的项目看起来像这样:
lx/ (project root)
src/
lx.c (contains main method)
conf.c
util/
str.c
str.h
etc.c
etc.h
server/
server.c
server.h
request.c
request.h
js/
js.c
js.h
interp.c
interp.h
bin/
lx (executable)
我应该怎么做?
答案 0 :(得分:70)
如果没有任何高于lx / src目录的源,那么就不需要 lx / CMakeLists.txt 文件。如果有,它应该看起来像这样:
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(lx)
add_subdirectory(src)
add_subdirectory(dir1)
add_subdirectory(dir2)
# And possibly other commands dealing with things
# directly in the "lx" directory
...在库依赖顺序中添加子目录。应首先添加依赖于其他任何内容的库,然后再添加依赖于这些库的库,依此类推。
<强> LX / SRC /的CMakeLists.txt 强>
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(lx_exe)
add_subdirectory(util)
add_subdirectory(js)
add_subdirectory(server)
set(lx_source_files conf.c lx.c)
add_executable(lx ${lx_source_files})
target_link_libraries(lx server)
# also transitively gets the "js" and "util" dependencies
<强> LX / SRC / util的/的CMakeLists.txt 强>
set(util_source_files
etc.c
etc.h
str.c
str.h
)
add_library(util ${util_source_files})
<强> LX / SRC / JS /的CMakeLists.txt 强>
set(js_source_files
interp.c
interp.h
js.c
js.h
)
add_library(js ${js_source_files})
target_link_libraries(js util)
<强> LX / SRC /服务器/的CMakeLists.txt 强>
set(server_source_files
request.c
request.h
server.c
server.h
)
add_library(server ${server_source_files})
target_link_libraries(server js)
# also transitively gets the "util" dependency
然后,在命令提示符下:
mkdir lx/bin
cd lx/bin
cmake ..
# or "cmake ../src" if the top level
# CMakeLists.txt is in lx/src
make
默认情况下,lx可执行文件将使用此精确布局结束在“lx / bin / src”目录中。您可以使用RUNTIME_OUTPUT_DIRECTORY目标属性和set_property命令控制最终的目录。
http://www.cmake.org/cmake/help/cmake-2-8-docs.html#prop_tgt:RUNTIME_OUTPUT_DIRECTORY
http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:set_property
如果lib通过add_library构建为CMake目标,则通过CMake目标名称引用target_link_libraries libs,否则通过库文件的完整路径引用。
另请参阅“cmake --help-command target_link_libraries”或任何其他cmake命令的输出,以及此处的cmake命令的完整联机文档:
http://www.cmake.org/cmake/help/cmake-2-8-docs.html#section_Commands
http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:target_link_libraries