繁体   English   中英

解决正交模块的依赖关系

[英]Resolving dependencies of orthogonal modules

我有一个由几个模块组成的项目,我想使用 CMake 的add_subdirectory function:

Project
+ CMakeLists.txt
+ bin
+  (stuff that depends on lib/)
+ lib
    module1
    + CMakeLists.txt
    + (cpp,hpp)
    module2
    + CMakeLists.txt
    + (cpp,hpp)
    module3
    + CMakeLists.txt
    + (cpp,hpp)
    logging.cpp
    logging.hpp

这些顶级模块相互独立,但都依赖于日志记录模块。 当我将相关的顶级模块代码从根CMakeLists移动到特定的子目录中时,我无法使用make编译它们,因为缺少日志记录模块。

有没有办法将日志模块的依赖编程到顶级模块的CMakeLists中,或者在根目录调用cmake时会自动解析吗?

有没有办法将日志记录模块的依赖项编程到顶级模块的 CMakeLists...

是的,您可以在根 CMakeLists.txt 文件中为日志记录功能定义 CMake 库目标。

CMakeLists.txt

cmake_minimum_required(VERSION 3.16)

project(MyBigProject)

# Tell CMake to build a shared library for the 'logging' functionality.
add_library(LoggingLib SHARED
    lib/logging.cpp
)
target_include_directories(LoggingLib PUBLIC ${CMAKE_SOURCE_DIR}/lib)

# Call add_subdirectory after defining the LoggingLib target.
add_subdirectory(lib/module1)
add_subdirectory(lib/module2)
add_subdirectory(lib/module3)

...

然后,只需将日志库目标链接到需要它的其他模块目标。 例如:

lib/module1/CMakeLists.txt

project(MyModule1)

# Tell CMake to build a shared library for the 'Module1' functionality.
add_library(Module1Lib SHARED
    ...
)

# Link the LoggingLib target to your Module1 library target.
target_link_libraries(Module1Lib PRIVATE LoggingLib)

请注意,这假设您从项目的根目录运行 CMake。 例如,如果您直接在lib/module1/CMakeLists.txt文件上运行 CMake,它将无法访问根CMakeLists.txt文件中定义的日志记录目标。

暂无
暂无

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

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