C语言 CMake下的多个目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6352123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple directories under CMake
提问by Aaron Yodaiken
I'm currently using recursive make and autotools and am looking to migrate to CMake for a project that looks something like this:
我目前正在使用递归 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)
How should I go about this?
我应该怎么做?
回答by DLRdave
If there's never any source higher than the lx/src directory, then there's no need for the lx/CMakeLists.txtfile. If there is, it should look something like this:
如果没有任何高于 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
...where the subdirectories are added in library dependency order. Libraries that depend on nothing else should be added first, and then libraries that depend on those, and so on.
...以库依赖顺序添加子目录的位置。应首先添加不依赖任何其他内容的库,然后添加依赖于这些的库,依此类推。
lx/src/CMakeLists.txt
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
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
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/server/CMakeLists.txt
lx/src/server/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
Then, in a command prompt:
然后,在命令提示符中:
mkdir lx/bin
cd lx/bin
cmake ..
# or "cmake ../src" if the top level
# CMakeLists.txt is in lx/src
make
By default, the lx executable will end up in the "lx/bin/src" directory using this exact layout. You can control what directory it ends up in by using the RUNTIME_OUTPUT_DIRECTORY target property and the set_property command.
默认情况下,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#prop_tgt:RUNTIME_OUTPUT_DIRECTORY
http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:set_property
http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:set_property
Refer to target_link_libraries libs either by CMake target name, if the lib is built as a CMake target via add_library, or by full path to the library file otherwise.
如果通过 add_library 将 lib 构建为 CMake 目标,或者通过库文件的完整路径,请通过 CMake 目标名称引用 target_link_libraries 库。
See also, the output of "cmake --help-command target_link_libraries", or any other cmake command, and the full online documentation for cmake commands found here:
另请参阅“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#section_Commands
http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:target_link_libraries
http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:target_link_libraries

