chr0x
chr0x

Reputation: 1251

CMake - Using my other projects code in my current project

I have a C++ project with this structure:

-- ProjectFolder
   -- Project_Prototype1
   -- Project_Prototype2

Project_Prototype1 has a CMakeLists file with this content:

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 is using an external library called FaceTracker .

In the Prototype2 I wanna access a file from Prototype1 but I don't want to redefine all dependencies from Prototype1 on Prototype2 CMakeLists (FaceTracker, Boost, etc...).

How can I write the Prototype2 CMakeLists to use Prototype1 without redefining everything manually?

Here is my current CMakeLists.txt for Prototype2:

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 )

Upvotes: 0

Views: 54

Answers (1)

Alain
Alain

Reputation: 572

The easiest way would be to create a file containing all the dependencies common to both CMakeList.txt files and use include(). For instance have a file names ProjectFolder/my-includes.cmake that contains the relevant find_package() statements, and in both CMakeList.txt files add a line

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

Everything defined in the included file is available to the including file.

Upvotes: 1

Related Questions