如何为具有多个子目录的大项目编写“CMakeLists.txt”?

我正在开发一个模拟项目:从目标平台获取嵌入式C代码库,并尝试在主机上模拟它以进行调试或单步执行代码.

操作系统: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 different CMakeLists.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})

相关文章

对象的传值与返回说起函数,就不免要谈谈函数的参数和返回值...
从实现装饰者模式中思考C++指针和引用的选择最近在看...
关于vtordisp知多少?我相信不少人看到这篇文章,多半是来自...
那些陌生的C++关键字学过程序语言的人相信对关键字并...
命令行下的树形打印最近在处理代码分析问题时,需要将代码的...
虚函数与虚继承寻踪封装、继承、多态是面向对象语言的三大特...