操作系统:Ubuntu Linux 14.04,IDE:CodeLite,Makefile Generator:Cmake.我对如何为项目编写CMakeLists.txt感到困惑.以下是代码库的结构(它全部用C语言编写):
|ARQSim\ |-->ARQSim.h |-->ARQSim.c |-->BaseStationCode\ | |->downlink.c | |->neoncopy.c | |->armCore\ | | |->common\ | | | |->Bsconfig.h | | | |->config.h | | |->MacSource\ | | | |->lib\ | | | | |->arqCommon.h | | | | |->OverTheAir.h | | | |->source\ | | | | |->beacon.c | | | | |->proxyDhcp.c | | | | |->ARQ\ | | | | | |->arqCommon.c | | | | | |->arqInterface.c | | | | | |->fragmentation\ | | | | | | |->fragBookkeeping.c | | | | | | |->fragProcessAck.c | | | | | |->reassembly\ | | | | | | |->reasmBookkeeping.c | | | | | | |->reasmProcessAck.c
我对Cmake完全不熟悉.我在StackOverflow上读了很多关于CMake和线程的资源.但我每次都感到困惑.我有几个问题:
>我是否只需要在根目录下的一个CMakeLists.txt或每个目录需要不同的CMakeLists.txt文件?
>如何在CMakeLists.txt中递归添加源文件?
>我需要在CMakeLists.txt中为MakeFile生成添加哪些基本命令?
任何反馈都非常感谢.特别是如果有人可以根据上面提到的代码结构给我一个例子.
谢谢.
解决方法
Do I need only one
CMakeLists.txt
at root directory or every directory needs a differentCMakeLists.txt
file?
您通常会在树的每个级别都有一个有意义的区域
例如:
root/ +--- CMakeLists.txt // your root CMakeLists +--- foo/ | +--- CMakeLists.txt // foo component's CMakeLists | +--- foo.c | +--- tests/ | +--- CMakeLists.txt // foo test's CMakeLists | +--- foo_tests.c +--- bar/ +--- CMakeLists.txt // bar component's CMakeLists +--- bar.c +--- bar_impl/ // no CMakeLists for this dir,it is part of bar | +--- bar_impl.c +--- tests/ +--- CMakeLists.txt // bar test's CMakeLists +--- bar_tests.c
项目根CMakeLists.txt:
在项目根目录CMakeLists.txt中,指定最小cmake要求,项目名称,并包含其中包含各种组件的子目录
根/的CMakeLists.txt:
cmake_minimum_required (VERSION 3.5) project (my_project C) add_subdirectory(foo) add_subdirectory(bar)
组件CMakeLists.txt:
然后在每个组件子目录中,您有另一个CMakeLists.txt文件,您可以在其中添加库,可执行文件等
根/富/的CMakeLists.txt:
add_library(foo foo.c) target_include_directories(foo PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) add_subdirectory(tests)
根/富/测试/的CMakeLists.txt:
add_executable(foo_test foo_tests.c) target_link_libraries(foo_test foo)
您可以按照此结构进行条形图等…
根/富/的CMakeLists.txt:
add_library(bar bar.c bar_impl/bar_impl.c) target_include_directories(bar PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) target_link_libraries(bar foo) add_subdirectory(tests)
根/酒吧/测试/的CMakeLists.txt:
add_executable(bar_test bar_tests.c) target_link_libraries(bar_test bar)
要引导构建,请将cmake指向root / CMakeLists.txt
cd root mkdir build cd build cmake ..
(或使用ide的构建管理器生成其构建配置)
进一步阅读
有关我在此处使用的各种功能的详细信息,请参阅文档:
> cmake_minimum_required
> project
> add_subdirectory
> target_include_directories
> target_link_libraries
最后,回答你的第二个问题:
How to add the source files recursively in CMakeLists.txt?
不建议这样做(有关详细信息,请参阅this discussion).
最好明确列出要包含在目标中的每个文件.
请注意,如果源文件位于多个单独的目录中,但它们都属于同一逻辑目标,则每个目录不需要CMakeLists.txt文件 – 只需列出文件名中的子目录
例:
foo/ +--- foo.c +--- bar.c +--- baz/ +--- baz.c +--- bang.c
如果您想要所有上述文件的单个目标foo,您可以按如下方式创建它:
add_library(foo foo.c bar.c baz/baz.c baz/bang.c)
或者,如果您真的想使用变量来存储SRCS列表
set(SRCS foo.c bar.c baz/baz.c baz/bang.c) add_library(foo ${SRCS})