代码之家  ›  专栏  ›  技术社区  ›  tsuki

解析正交模的依赖关系

  •  0
  • tsuki  · 技术社区  · 4 年前

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

    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 因为缺少日志模块。

    是否有方法将日志模块的依赖关系编程到 CMake列表 顶级模块的问题,或者在调用时会自动解决吗 cmake 在根目录中?

    0 回复  |  直到 4 年前
        1
  •  2
  •   Kevin Boris Azanov    4 年前

    是否有方法将日志模块的依赖关系编程到顶级模块的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/cCMakeLists.txt 例如,它将 有权访问根目录中定义的日志记录目标 CMakeLists.txt 文件。