gpt4 book ai didi

c - 有多个子目录的大项目怎么写 "CMakeLists.txt"?

转载 作者:IT王子 更新时间:2023-10-29 00:11:40 27 4
gpt4 key购买 nike

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

操作系统:Ubuntu Linux 14.04,IDE:CodeLite,Makefile 生成器: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 和线程的资源。但我每次都感到困惑。我有几个问题:

  1. 我是在根目录下只需要一个 CMakeLists.txt,还是每个目录都需要一个不同的 CMakeLists.txt 文件?
  2. 如何在CMakeLists.txt中递归添加源文件?
  3. 我需要将哪些基本命令放入 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 要求、项目名称,并包含包含各种组件的子目录

root/CMakeLists.txt:

cmake_minimum_required (VERSION 3.5)
project (my_project C)

add_subdirectory(foo)
add_subdirectory(bar)

组件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)

root/foo/tests/CMakeLists.txt:

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

你按照这个结构来做 bar 等......

root/bar/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)

root/bar/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 的构建管理器生成其构建配置)

进一步阅读

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

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

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 - 有多个子目录的大项目怎么写 "CMakeLists.txt"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47162766/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com