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

前端之家收集整理的这篇文章主要介绍了如何为具有多个子目录的大项目编写“CMakeLists.txt”?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个模拟项目:从目标平台获取嵌入式C代码库,并尝试在主机上模拟它以进行调试或单步执行代码.

操作系统:Ubuntu Linux 14.04,IDE:CodeLite,Makefile Generator:Cmake.我对如何为项目编写CMakeLists.txt感到困惑.以下是代码库的结构(它全部用C语言编写):

  1. |ARQSim\
  2. |-->ARQSim.h
  3. |-->ARQSim.c
  4. |-->BaseStationCode\
  5. | |->downlink.c
  6. | |->neoncopy.c
  7. | |->armCore\
  8. | | |->common\
  9. | | | |->Bsconfig.h
  10. | | | |->config.h
  11. | | |->MacSource\
  12. | | | |->lib\
  13. | | | | |->arqCommon.h
  14. | | | | |->OverTheAir.h
  15. | | | |->source\
  16. | | | | |->beacon.c
  17. | | | | |->proxyDhcp.c
  18. | | | | |->ARQ\
  19. | | | | | |->arqCommon.c
  20. | | | | | |->arqInterface.c
  21. | | | | | |->fragmentation\
  22. | | | | | | |->fragBookkeeping.c
  23. | | | | | | |->fragProcessAck.c
  24. | | | | | |->reassembly\
  25. | | | | | | |->reasmBookkeeping.c
  26. | | | | | | |->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?

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

例如:

  1. root/
  2. +--- CMakeLists.txt // your root CMakeLists
  3. +--- foo/
  4. | +--- CMakeLists.txt // foo component's CMakeLists
  5. | +--- foo.c
  6. | +--- tests/
  7. | +--- CMakeLists.txt // foo test's CMakeLists
  8. | +--- foo_tests.c
  9. +--- bar/
  10. +--- CMakeLists.txt // bar component's CMakeLists
  11. +--- bar.c
  12. +--- bar_impl/ // no CMakeLists for this dir,it is part of bar
  13. | +--- bar_impl.c
  14. +--- tests/
  15. +--- CMakeLists.txt // bar test's CMakeLists
  16. +--- bar_tests.c

项目根CMakeLists.txt:

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

根/的CMakeLists.txt:

  1. cmake_minimum_required (VERSION 3.5)
  2. project (my_project C)
  3.  
  4. add_subdirectory(foo)
  5. add_subdirectory(bar)

组件CMakeLists.txt:

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

根/富/的CMakeLists.txt:

  1. add_library(foo foo.c)
  2. target_include_directories(foo PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
  3.  
  4. add_subdirectory(tests)

根/富/测试/的CMakeLists.txt:

  1. add_executable(foo_test foo_tests.c)
  2. target_link_libraries(foo_test foo)

您可以按照此结构进行条形图等…

根/富/的CMakeLists.txt:

  1. add_library(bar
  2. bar.c
  3. bar_impl/bar_impl.c)
  4. target_include_directories(bar PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
  5. target_link_libraries(bar foo)
  6.  
  7. add_subdirectory(tests)

根/酒吧/测试/的CMakeLists.txt:

  1. add_executable(bar_test bar_tests.c)
  2. target_link_libraries(bar_test bar)

生成构建文件

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

  1. cd root
  2. mkdir build
  3. cd build
  4. 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文件 – 只需列出文件名中的子目录

例:

  1. foo/
  2. +--- foo.c
  3. +--- bar.c
  4. +--- baz/
  5. +--- baz.c
  6. +--- bang.c

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

  1. add_library(foo
  2. foo.c
  3. bar.c
  4. baz/baz.c
  5. baz/bang.c)

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

  1. set(SRCS
  2. foo.c
  3. bar.c
  4. baz/baz.c
  5. baz/bang.c)
  6.  
  7. add_library(foo ${SRCS})

猜你在找的C&C++相关文章