如果没有比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/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
然后,在命令提示符中:
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 target属性和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个
如果库通过add_library构建为CMake目标,则可以通过CMake目标名称引用target_link_library库,否则可以通过库文件的完整路径引用.
另请参见"cmake--help-command target_link_library"或任何其他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个