Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 正交模依赖关系的求解_C++_Cmake - Fatal编程技术网

C++ 正交模依赖关系的求解

C++ 正交模依赖关系的求解,c++,cmake,C++,Cmake,我有一个由几个模块组成的项目,我想使用CMake的add_子目录功能: 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

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

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
时自动解决

有没有办法将日志模块的依赖项编程到顶级模块的CmakeList中

是的,您可以在根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)

...
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)
然后,只需将日志库目标链接到需要它的其他模块目标。例如:

lib/module1/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)

...
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
文件中定义的日志记录目标。

@squareskittles我能够解决我的问题,您的评论对我帮助很大。如果你把它作为一个答案发布,我会接受的。我写了一个例子,展示了你如何做到这一点,假设你如何在你的项目上运行CMake。希望对你有帮助!