【发布时间】:2015-12-14 18:48:47
【问题描述】:
我的代码是这样组织的:
- cpp
- main.cpp(从
dataStructures/和common/调用代码) - CMakeLists.txt(最顶层 CMakeLists 文件)
- 构建
- 常见
- CMakeLists.txt(应该负责构建公共共享库)
- 包括
- utils.h
- 源
- utils.cpp
- 构建
- 数据结构
- CMakeLists.txt(构建 dataStructures 共享库 - 依赖于公共库)
- 包括
- dsLinkedList.h
- 源
- dsLinkedList.cpp
- 构建
- main.cpp(从
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 以进入子目录(common 和 dataStructures)并在尚未构建目标的情况下构建它们,而无需手动构建各个库?
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
【问题讨论】: