c++ - 如何使用 cmake 在子目录中构建库?

标签 c++ cmake

我的代码是这样组织的:

  • cpp
    • main.cpp(从 dataStructures/common/ 调用代码)
    • CMakeLists.txt(最顶层 CMakeLists 文件)
    • build
    • 常见的
      • CMakeLists.txt(应负责构建公共(public)共享库)
      • 包括
        • utils.h
      • 来源
        • 实用程序.cpp
      • build
    • 数据结构
      • CMakeLists.txt(构建 dataStructures 共享库 - 依赖于公共(public)库)
      • 包括
        • dsLinkedList.h
      • 来源
        • dsLinkedList.cpp
      • build

build\ 目录包含构建的目标。实际代码可以在这里看到:https://github.com/brainydexter/PublicCode/tree/master/cpp

截至目前,每个子目录中的 CMakeLists.txt 都构建了自己的共享库。最顶层的 CMakeLists 文件然后像这样引用库和路径

最顶层的 CMakeLists.txt

cmake_minimum_required(VERSION 3.2.2)
project(cpp)

#For the shared library:
set ( PROJECT_LINK_LIBS libcppDS.dylib libcppCommon.dylib)
link_directories( dataStructures/build )
link_directories( common/build )

#Bring the headers, into the project
include_directories(common/include)
include_directories(dataStructures/include)

#Can manually add the sources using the set command as follows:
set(MAINEXEC main.cpp)

add_executable(testDS ${MAINEXEC})
target_link_libraries(testDS ${PROJECT_LINK_LIBS} )

如果尚未构建目标,我如何更改最顶层的 CMakeLists.txt 以进入子目录(commondataStructures)并构建它们的目标,无需我手动构建各个库?

common 的 CMakeLists:

cmake_minimum_required(VERSION 3.2.2)
project(cpp_common)
set(CMAKE_BUILD_TYPE Release)

#Bring the headers, such as Student.h into the project
include_directories(include)

#However, the file(GLOB...) allows for wildcard additions:
file(GLOB SOURCES "src/*.cpp")

#Generate the shared library from the sources
add_library(cppCommon SHARED ${SOURCES})

数据结构:

cmake_minimum_required(VERSION 3.2.2)
project(cpp_dataStructures)
set(CMAKE_BUILD_TYPE Release)

#For the shared library:
set ( PROJECT_LINK_LIBS libcppCommon.dylib )
link_directories( ../common/build )

#Bring the headers, such as Student.h into the project
include_directories(include)
include_directories(../common/include/)

#However, the file(GLOB...) allows for wildcard additions:
file(GLOB SOURCES "src/*.cpp")

#Generate the shared library from the sources
add_library(cppDS SHARED ${SOURCES})

更新:

这个拉取请求帮助我理解了这样做的正确方法: https://github.com/brainydexter/PublicCode/pull/1

和 commitId:4b4f1d3d24b5d82f78da3cbffe423754d8c39ec0 on my git

最佳答案

您只缺少一件简单的事情:add_subdirectory。 来自文档:

add_subdirectory(source_dir [binary_dir] [EXCLUDE_FROM_ALL])

Add a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.txt and code files are located. If it is a relative path it will be evaluated with respect to the current directory (the typical usage), but it may also be an absolute path. The binary_dir specifies the directory in which to place the output files. If it is a relative path it will be evaluated with respect to the current output directory, but it may also be an absolute path.

http://www.cmake.org/cmake/help/v3.0/command/add_subdirectory.html

它完全可以满足您的需求。

关于c++ - 如何使用 cmake 在子目录中构建库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32638141/

相关文章:

c++ - 在 CMake 中使用预编译的头文件

ios - 如何为 iOS 构建 DLIB

c++ - Cmake:如何将库标记为依赖于系统库

c++ - 基于CMake编译C++项目时"fatal error: ' chrono ' file not found"

c++ - VS2019 c++深度继承模板中的C3861错误

c++ - 在代码中修改预处理器宏是一种好的风格吗?

c++ - 错误 : prototype for ‘XXX’ does not match any in class ‘YYY’

c++ - Qt - 显示选择顶部的结果

c++ - 我是否必须使用 std::shared_ptr 删除对对象的所有引用

cmake 别名的用处