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

Kaz*_*san 11 c linux makefile cmake codelite

我正在开发一个模拟项目:从目标平台获取嵌入式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
Run Code Online (Sandbox Code Playgroud)

我对Cmake完全不熟悉.我在StackOverflow上读了很多关于CMake和线程的资源.但我每次都感到困惑.我有几个问题:

  1. 我是否只需要在根目录下有一个CMakeLists.txt,或者每个目录是否需要不同的CMakeLists.txt文件?
  2. 如何在CMakeLists.txt中递归添加源文件?
  3. 我需要在CMakeLists.txt中为MakeFile生成添加哪些基本命令?

任何反馈都非常感谢.特别是如果有人可以根据上面提到的代码结构给我一个例子.谢谢.

Ste*_*mer 16

我是否只需要一个CMakeLists.txt根目录或每个目录需要一个不同的CMakeLists.txt文件?

您通常会在树的每个级别都有一个有意义的地方

例如:

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
Run Code Online (Sandbox Code Playgroud)

项目根CMakeLists.txt:

在项目根目录中CMakeLists.txt,指定最小cmake要求,项目名称,并包含其中包含各种组件的子目录

root/CMakeLists.txt:

cmake_minimum_required (VERSION 3.5)
project (my_project C)

add_subdirectory(foo)
add_subdirectory(bar)
Run Code Online (Sandbox Code Playgroud)

组件CMakeLists.txt:

然后在每个组件子目录中,您有另一个CMakeLists.txt文件,您可以在其中添加库,可执行文件等

root/foo/CMakeLists.txt:

add_library(foo foo.c)
target_include_directories(foo PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

add_subdirectory(tests)
Run Code Online (Sandbox Code Playgroud)

root/foo/tests/CMakeLists.txt:

add_executable(foo_test foo_tests.c)
target_link_libraries(foo_test foo)
Run Code Online (Sandbox Code Playgroud)

您可以按照此结构进行操作等...

root/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)
Run Code Online (Sandbox Code Playgroud)

root/bar/tests/CMakeLists.txt:

add_executable(bar_test bar_tests.c)
target_link_libraries(bar_test bar)
Run Code Online (Sandbox Code Playgroud)

生成构建文件:

要引导您的构建,请将cmake指向您的 root/CMakeLists.txt

cd root
mkdir build
cd build
cmake ..
Run Code Online (Sandbox Code Playgroud)

(或使用ide的构建管理器生成其构建配置)

进一步阅读

有关我在此处使用的各种功能的详细信息,请参阅文档:

最后,回答你的第二个问题:

如何在CMakeLists.txt中递归添加源文件?

建议不要这样做(有关详细信息,请参阅此讨论).

最好明确列出要包含在目标中的每个文件.

请注意,如果源文件位于多个单独的目录中,但它们都属于同一个逻辑目标,那么CMakeLists.txt每个目录都不需要一个文件 - 只需列出文件名中的子目录

例:

foo/
+--- foo.c
+--- bar.c
+--- baz/
     +--- baz.c
     +--- bang.c
Run Code Online (Sandbox Code Playgroud)

如果您想要foo所有上述文件的单个目标,您可以按如下方式创建它:

add_library(foo 
   foo.c
   bar.c
   baz/baz.c
   baz/bang.c)
Run Code Online (Sandbox Code Playgroud)

或者,如果您真的想使用变量来存储列表 SRCS

set(SRCS 
   foo.c
   bar.c
   baz/baz.c
   baz/bang.c)

add_library(foo ${SRCS})
Run Code Online (Sandbox Code Playgroud)