我有一个具有以下结构的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 CMakeList以使用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 )
最佳答案
最简单的方法是创建一个包含所有CMakeList.txt文件共有的所有依赖项的文件,并使用include()。例如,有一个文件名ProjectFolder / my-includes.cmake,其中包含相关的find_package()语句,并且在两个CMakeList.txt文件中都添加了一行
include ("../my-includes.cmake")
包含文件中定义的所有内容均可用于包含文件。
关于c++ - CMake-在当前项目中使用其他项目代码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34633842/