使用Cmake构建依赖关系

时间:2016-04-21 20:50:04

标签: c++ cmake

我是CMake的新手,我正试图弄清楚如何构建依赖关系。我的项目文件夹的组织方式如下:

Scrubber
 -- FileIO
 -- CDEs
 -- Utilities
 -- Scrubber

FileIO,CDE和Utilities是Scrubber中可执行文件使用的静态库。

我希望能够从顶层目录执行一个make命令来构建所有内容。如果我独立构建每个库,那么当我执行top make时,它们会很好地结合在一起。但如果我没有提前做到这一点,那么它就不会构建依赖关系,并且毫不奇怪地抱怨图书馆找不到。

这么简单的问题:如何让系统构建库?

SCRUBBER的最高级别的CMAKELISTS

cmake_minimum_required(VERSION 3.2)
project(Scrubber)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

add_subdirectory(FileIO)
add_subdirectory(CDEs)
add_subdirectory(Utilities)
add_subdirectory(Scrubber)

SCRUBBER / FILEIO中的FILEIO CMAKELISTS

cmake_minimum_required(VERSION 3.2)
project(FileIO)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

file(GLOB SOURCE_FILES *.cpp)

add_library(FileIO STATIC ${SOURCE_FILES})

#Don't prepend with "lib"
set_target_properties(FileIO PROPERTIES PREFIX "")

CDES CMAKELISTS IN SCRUBBER / CDES

cmake_minimum_required(VERSION 3.2)
project(CDEs)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(BOOST_INCLUDE_DIR "/usr/lib/boost_1_60_0")

include_directories( ${BOOST_INCLUDE_DIR} )

file(GLOB SOURCE_FILES *.cpp)

add_library(CDEs STATIC ${SOURCE_FILES})

#Don't prepend with "lib"
set_target_properties(CDEs PROPERTIES PREFIX "")

洗涤器/实用工具中的实用工具

cmake_minimum_required(VERSION 3.2)
project(Utilities)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(BOOST_INCLUDE_DIR "/usr/lib/boost_1_60_0")

include_directories( ${BOOST_INCLUDE_DIR} )

file(GLOB SOURCE_FILES *.cpp)

add_library(Utilities STATIC ${SOURCE_FILES})

#Don't prepend with "lib"
set_target_properties(Utilities PROPERTIES PREFIX "")

SCRUBBER / SCRUBBER中的SCRUBBER CMAKELIST

cmake_minimum_required(VERSION 3.2)
project(Scrubber)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(BOOST_INCLUDE_DIR "/usr/lib/boost_1_60_0")
set(PROJECT_INCLUDE_DIR "..")

include_directories(${BOOST_INCLUDE_DIR} ${PROJECT_INCLUDE_DIR})

find_library(FILEIO_LIB FileIO.a HINTS ../FileIO/)
find_library(CDES_LIB CDEs.a HINTS ../CDEs/)
find_library(UTILITIES_LIB Utilities.a HINTS ../Utilities/)

file(GLOB SOURCE_FILES *.cpp)

add_executable(Scrubber ${SOURCE_FILES})

target_link_libraries(Scrubber ${FILEIO_LIB} ${CDES_LIB} ${UTILITIES_LIB})

2 个答案:

答案 0 :(得分:0)

CMake包含完全符合您需要的add_dependency命令。

  

使顶级依赖于其他顶级目标,以确保它们之前构建。

答案 1 :(得分:0)

这是错误的:

target_link_libraries(Scrubber ${FILEIO_LIB} ${CDES_LIB} ${UTILITIES_LIB})

如果你有图书馆add_library(FileIO STATIC ${SOURCE_FILES}),那么你应该 写:

target_link_libraries(Scrubber FileIO )

cmake发现这也是目标,并创建具有适当依赖关系的make文件,并且您不必指向add_dependency的任何依赖项, cmake自动执行此操作。至少对我来说是cmake版本3.5