CMake并查找其他项目及其依赖项


76

设想以下情形:项目A是一个共享库,它具有多个依赖项(LibA,LibB和LibC)。项目B是一个依赖于项目A的可执行文件,因此还需要所有项目A的依赖项才能进行构建。

此外,两个项目都是使用CMake构建的,并且不需要为通过项目B使用而安装项目A(通过“安装”目标),因为这可能会对开发人员造成麻烦。

使用CMake解决这些依赖性的最佳方法是什么?理想的解决方案将是尽可能简单(尽管没有更简单)并且需要最少的维护。



1
该教程似乎没有解释如何处理导出外部库依赖项。与之链接的唯一库是该项目构建的库。我需要知道如何告诉项目B项目A需要各种外部库等这些需要被添加到项目B的链接步骤
奔农民

实际上,如果您是PC专家,则应该尝试Linux或Linux子系统。这个平台最好的是Linux将为您安装所有依赖项。或者更好的是,它建议您缺少哪些依赖项,并提供Sudo apt-get install mydependencies,如何安装。真的很容易。
Juniar

@Juniar,我确实确实简化和优化了很多事情。但是使软件部署成为一场噩梦。我希望将所有软件打包在一个软件包中,然后全部部署(甚至部分复制某些库)。更不用说维护问题了。每个盒子都有一套独特的库(在某种程度上)。
OpalApps

@OpalApps,依赖关系可能安装在不同的路径和目录中,但是您仍然可以在编译时添加这些依赖关系,或者配置/包括外部路径。它们不会全部安装在一个路径上True,但是“ sudo apt-get install”确实安装在特定目录上,只需切换它们即可。
Juniar

Answers:


146

简单。这是我脑海中的例子:

最高层CMakeLists.txt

cmake_minimum_required(VERSION 2.8.10)

# You can tweak some common (for all subprojects) stuff here. For example:

set(CMAKE_DISABLE_IN_SOURCE_BUILD ON)
set(CMAKE_DISABLE_SOURCE_CHANGES  ON)

if ("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_BINARY_DIR}")
  message(SEND_ERROR "In-source builds are not allowed.")
endif ()

set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_COLOR_MAKEFILE   ON)

# Remove 'lib' prefix for shared libraries on Windows
if (WIN32)
  set(CMAKE_SHARED_LIBRARY_PREFIX "")
endif ()

# When done tweaking common stuff, configure the components (subprojects).
# NOTE: The order matters! The most independent ones should go first.
add_subdirectory(components/B) # B is a static library (depends on Boost)
add_subdirectory(components/C) # C is a shared library (depends on B and external XXX)
add_subdirectory(components/A) # A is a shared library (depends on C and B)

add_subdirectory(components/Executable) # Executable (depends on A and C)

CMakeLists.txtcomponents/B

cmake_minimum_required(VERSION 2.8.10)

project(B C CXX)

find_package(Boost
             1.50.0
             REQUIRED)

file(GLOB CPP_FILES source/*.cpp)

include_directories(${Boost_INCLUDE_DIRS})

add_library(${PROJECT_NAME} STATIC ${CPP_FILES})

# Required on Unix OS family to be able to be linked into shared libraries.
set_target_properties(${PROJECT_NAME}
                      PROPERTIES POSITION_INDEPENDENT_CODE ON)

target_link_libraries(${PROJECT_NAME})

# Expose B's public includes (including Boost transitively) to other
# subprojects through cache variable.
set(${PROJECT_NAME}_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/include
                                 ${Boost_INCLUDE_DIRS}
    CACHE INTERNAL "${PROJECT_NAME}: Include Directories" FORCE)

CMakeLists.txtcomponents/C

cmake_minimum_required(VERSION 2.8.10)

project(C C CXX)

find_package(XXX REQUIRED)

file(GLOB CPP_FILES source/*.cpp)

add_definitions(${XXX_DEFINITIONS})

# NOTE: Boost's includes are transitively added through B_INCLUDE_DIRS.
include_directories(${B_INCLUDE_DIRS}
                    ${XXX_INCLUDE_DIRS})

add_library(${PROJECT_NAME} SHARED ${CPP_FILES})

target_link_libraries(${PROJECT_NAME} B
                                      ${XXX_LIBRARIES})

# Expose C's definitions (in this case only the ones of XXX transitively)
# to other subprojects through cache variable.
set(${PROJECT_NAME}_DEFINITIONS ${XXX_DEFINITIONS}
    CACHE INTERNAL "${PROJECT_NAME}: Definitions" FORCE)

# Expose C's public includes (including the ones of C's dependencies transitively)
# to other subprojects through cache variable.
set(${PROJECT_NAME}_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/include
                                 ${B_INCLUDE_DIRS}
                                 ${XXX_INCLUDE_DIRS}
    CACHE INTERNAL "${PROJECT_NAME}: Include Directories" FORCE)

CMakeLists.txtcomponents/A

cmake_minimum_required(VERSION 2.8.10)

project(A C CXX)

file(GLOB CPP_FILES source/*.cpp)

# XXX's definitions are transitively added through C_DEFINITIONS.
add_definitions(${C_DEFINITIONS})

# NOTE: B's and Boost's includes are transitively added through C_INCLUDE_DIRS.
include_directories(${C_INCLUDE_DIRS})

add_library(${PROJECT_NAME} SHARED ${CPP_FILES})

# You could need `${XXX_LIBRARIES}` here too, in case if the dependency 
# of A on C is not purely transitive in terms of XXX, but A explicitly requires
# some additional symbols from XXX. However, in this example, I assumed that 
# this is not the case, therefore A is only linked against B and C.
target_link_libraries(${PROJECT_NAME} B
                                      C)

# Expose A's definitions (in this case only the ones of C transitively)
# to other subprojects through cache variable.
set(${PROJECT_NAME}_DEFINITIONS ${C_DEFINITIONS}
    CACHE INTERNAL "${PROJECT_NAME}: Definitions" FORCE)

# Expose A's public includes (including the ones of A's dependencies
# transitively) to other subprojects through cache variable.
set(${PROJECT_NAME}_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/include
                                 ${C_INCLUDE_DIRS}
    CACHE INTERNAL "${PROJECT_NAME}: Include Directories" FORCE)

CMakeLists.txtcomponents/Executable

cmake_minimum_required(VERSION 2.8.10)

project(Executable C CXX)

file(GLOB CPP_FILES source/*.cpp)

add_definitions(${A_DEFINITIONS})

include_directories(${A_INCLUDE_DIRS})

add_executable(${PROJECT_NAME} ${CPP_FILES})

target_link_libraries(${PROJECT_NAME} A C)

为了清楚起见,这是相应的源树结构:

Root of the project
├───components
│   ├───Executable
│   │   ├───resource
│   │   │   └───icons
│   │   ├───source
|   |   └───CMakeLists.txt
│   ├───A
│   │   ├───include
│   │   │   └───A
│   │   ├───source
|   |   └───CMakeLists.txt
│   ├───B
│   │   ├───include
│   │   │   └───B
│   │   ├───source
|   |   └───CMakeLists.txt
│   └───C
│       ├───include
│       │   └───C
│       ├───source
|       └───CMakeLists.txt
└───CMakeLists.txt

在很多方面都可以对其进行调整/定制或更改,以满足某些需求,但这至少应该可以帮助您入门。

注意:我已经在多个中型和大型项目中成功采用了这种结构。


15
您是F ****星!你真的让我摆脱了持续了将近一天的巨大头痛。非常感谢1
rsacchettini

2
我很好奇,如果我要从“可执行文件”目录中调用Cmake,它会编译吗?还是应该始终从项目的根目录进行编译?
Ahmet Ipkin '16

1
我认为这样做确实有一个小缺点,即您在每个项目中两次定义了include目录(一次为set(...INCLUDE_DIRS,一次为include_directories()),但我很难维护(总是记得在两个地方添加一个新的include依赖项)。你可以和他们讨好get_property(...PROPERTY INCLUDE_DIRECTORIES)
pseyfert

2
这确实很棒,但是当A,B和C是完全独立的项目时,如何实现同一目标呢?即我想构建A并将其导出以具有自己的ProjectConfig.cmake文件,然后在B中使用find_package在系统上查找A,以某种方式获取A所依赖的所有库的列表,以便在链接时可以链接它们建筑物B
奔农民

2
@Ben Farmer,这的确是更复杂的主题,值得撰写大量文章以正确解释。我从来没有足够的耐心在这里概述它。实际上,这就是我管理项目的工作,因为这实际上是CMake的最终(专业)用途和意图。为了管理所有这些,我有自己的CMake框架,该框架在后台处理了很多事情。例如,您可以尝试构建我的C ++ HacksC ++ Firewall玩具项目。
亚历山大·舒卡耶夫

14

亚历山大·舒卡耶夫(Alexander Shukaev)的事业有不错的开端,但仍有许多事情可以做得更好:

  1. 不要使用include_directories。至少要使用target_include_directories。但是,如果使用导入的目标,甚至可能甚至不需要这样做。
  2. 使用导入的目标。Boost的示例:

    find_package(Boost 1.56 REQUIRED COMPONENTS
                 date_time filesystem iostreams)
    add_executable(foo foo.cc)
    target_link_libraries(foo
      PRIVATE
        Boost::date_time
        Boost::filesystem
        Boost::iostreams
    )
    

    这将处理include目录,库等。如果在B的标头中使用Boost,则使用PUBLIC而不是PRIVATE,并且这些依赖项将被可传递地添加到依赖于B的任何对象上。

  3. 不要使用文件搜索(除非您使用3.12)。直到最近,文件关联仅在配置期间起作用,因此,如果添加文件并进行构建,则在您明确地重新生成项目之前,它无法检测到更改。但是,如果直接列出文件并尝试进行构建,则它应该识别出配置已过时并在构建步骤中自动重新生成。

这里有个好话题(YouTube):C ++ Now 2017:Daniel Pfeifer“有效的CMake”

其中涵盖了允许您的根级CMake与find_packageOR一起使用的软件包管理器构想subdirectory,但是,我一直在尝试采用这种构想,并且在使用find_package所有东西以及拥有像您这样的目录结构时遇到了很大的问题。


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.