简体   繁体   English

如何处理 CMake 中的子目录依赖关系?

[英]How do I handle subdirectory dependencies in CMake?

I have a project where I need to run one application on multiple boards.我有一个项目需要在多个板上运行一个应用程序。 To do this I have two exectuables that share a main.c , but are compiled with a different board.c .为此,我有两个共享一个main.c的可执行文件,但使用不同的board.c编译。 The directory tree is as follows:目录树如下:

CMakeLists.txt
src/
├─ board/
│  ├─ board_a/
│  │  ├─ board.c
|  |  ├─ CMakeLists.txt
│  ├─ board_b/
│  │  ├─ board.c
|  |  ├─ CMakeLists.txt
├─ app/
│  ├─ main.c
├─ lib/
│  ├─ somelib/
│  │  ├─ somelib.h
│  │  ├─ somelib.c

In an attempt to make the root CMakeLists.txt smaller and more maintainable, I've created a CMakeLists.txt for both boards, and would like to compile each board as an object library for the main executable in the root CMakeLists.txt to add_subdirectory on and link in.为了使根CMakeLists.txt更小且更易于维护,我为两个板创建了一个CMakeLists.txt ,并希望将每个板编译为 object 库,用于根CMakeLists.txtadd_subdirectory中的主要可执行文件打开并链接。

My problem is that board.c (as well as main.c ) depends on somelib .我的问题是board.c (以及main.c )取决于somelib How can I add this dependency to the subdirectory CMakeLists.txt ?如何将此依赖项添加到子目录CMakeLists.txt Is there a way I can do that without hard-coding a path to somelib ?有没有办法在不硬编码somelib路径的情况下做到这一点? My feeling is that I should create a CMakeLists.txt in somelib , and I feel this would be easy if I were handling the library linking from the root CMakeLists.txt , but my confusion is that board is adjacent to lib .我的感觉是我应该在somelib中创建一个CMakeLists.txt ,如果我处理从根CMakeLists.txt链接的库,我觉得这会很容易,但我的困惑是boardlib相邻。 I'm relatively new to CMake and am not sure how to best structure these dependencies.我对 CMake 比较陌生,并且不确定如何最好地构建这些依赖项。

My problem is that board.c (as well as main.c) depends on somelib.我的问题是board.c(以及main.c)依赖于somelib。 How can I add this dependency to the subdirectory CMakeLists.txt?如何将此依赖项添加到子目录 CMakeLists.txt? Is there a way I can do that without hard-coding a path to somelib?有没有一种方法可以在不硬编码到 somelib 的路径的情况下做到这一点? My feeling is that I should create a CMakeLists.txt in somelib, and I feel this would be easy if I were handling the library linking from the root CMakeLists.txt, but my confusion is that board is adjacent to lib.我的感觉是我应该在 somelib 中创建一个 CMakeLists.txt,如果我处理从根 CMakeLists.txt 链接的库,我觉得这会很容易,但我的困惑是板与 lib 相邻。 I'm relatively new to CMake and am not sure how to best structure these dependencies.我对 CMake 比较陌生,并且不确定如何最好地构建这些依赖项。

This is very straightforward start by linking each board_<X> to somelib 's target.通过将每个board_<X>链接到somelib的目标,这是非常简单的开始。

# in each board_<X>/CMakeLists.txt

add_library(board_<X> OBJECT ...)
target_link_libraries(board_<X> PUBLIC proj::somelib)

then create the target for somelib :然后为somelib创建目标:

# src/lib/somelib/CMakeLists.txt

add_library(somelib somelib.c)
add_library(proj::somelib ALIAS somelib)

target_include_directories(
  somelib PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>")

As long as the top-level CMakeLists.txt includes these subdirectories via add_subdirectory (multiple levels is fine), then other targets can #include <somelib.h> and CMake will handle linking.只要顶层 CMakeLists.txt 通过add_subdirectory包含这些子目录(多个级别都可以),那么其他目标可以#include <somelib.h>和 CMake 将处理链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM