CMake - 在我当前的项目中使用我的其他项目代码

时间:2016-01-06 13:01:56

标签: c++ cmake

我有一个具有这种结构的C ++项目:

-- ProjectFolder
   -- Project_Prototype1
   -- Project_Prototype2

Project_Prototype1有一个包含此内容的CMakeLists文件:

cmake_minimum_required(VERSION 2.8)
project( Neuromarketing-RF )
find_package( Boost 1.58.0 REQUIRED COMPONENTS filesystem system )
include_directories( ${Boost_INCLUDE_DIRS} include src/external )
find_package( OpenCV 3 REQUIRED )

file(GLOB FACETRACKER_HEADERS "external/FaceTracker/include/FaceTracker/*.h")
file(GLOB FACETRACKER_SRC "external/FaceTracker/src/lib/*.cc")
file(GLOB SOURCES "src/*cpp")

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "bin/")
add_executable( Neuromarketing-RF ${FACETRACKER_SRC} ${SOURCES} )
target_link_libraries( Neuromarketing-RF ${OpenCV_LIBS} ${Boost_LIBRARIES} )
target_compile_features( Neuromarketing-RF PRIVATE cxx_range_for )

Prototype1正在使用名为FaceTracker的外部库。

在Prototype2中,我想从Prototype1访问一个文件,但我不想在Prototype2 CMakeLists(FaceTracker,Boost等等)上重新定义Prototype1的所有依赖项。

如何在不重新定义所有内容的情况下编写Prototype2 CMakeLists以使用Prototype1?

这是我目前针对Prototype2的CMakeLists.txt:

cmake_minimum_required (VERSION 2.6)
project (Neuromarketing-CNN) 
find_package(OpenCV REQUIRED)
find_package(Boost 1.58.0 REQUIRED COMPONENTS filesystem system) 

add_subdirectory("../Neuromarketing-RF" "../Neuromarketing-RF/bin" ) 

file ( GLOB NM_RF_SRC "../Neuromarketing-RF/src/*.cpp" ) 
include_directories(include ../Neuromarketing-RF/include ${Boost_INCLUDE_DIRS} )  
file(GLOB SOURCES "src/*.cpp")
add_executable (bin/Neuromarketing-CNN ${SOURCES} ${NM_RF_SRC} )
target_link_libraries(bin/Neuromarketing-CNN ${OpenCV_LIBS} ${Boost_LIBRARIES} )
target_compile_features( bin/Neuromarketing-CNN PRIVATE cxx_range_for )

1 个答案:

答案 0 :(得分:1)

最简单的方法是创建一个包含CMakeList.txt文件共有的所有依赖项的文件,并使用 include()。例如,文件名 ProjectFolder / my-includes.cmake 包含相关的find_package()语句,并在两个CMakeList.txt文件中添加一行

include ("../my-includes.cmake")

包含文件中定义的所有内容都可用于包含文件。