guinny
guinny

Reputation: 1532

cmake: hierarchical project setup

I have a project with the following structure:

proj:
-CMakeLists.txt    
-subdir0   
  -CMakeLists.txt    
  -app0.cpp
  -app1.cpp
-subdir1
  -CMakeLists.txt    
  -app2.cpp

And after build, I like to have:

proj:    
-CMakeLists.txt    
-subdir0   
  -CMakeLists.txt    
  -app0.cpp
  -app1.cpp
-subdir1
  -CMakeLists.txt    
  -app2.cpp
-build
  -subdir0   
    -app0.exec
    -app1.exec
  -subdir1
    -app2.exec

The CMake doc is quite difficult to read and all I need here is an example (e.g. an existing project) how to set this up...

thanks a lot!

Upvotes: 12

Views: 5960

Answers (1)

Fraser
Fraser

Reputation: 78280

You want the following:

proj/CMakeLists.txt:

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(MyTest)
add_subdirectory(subdir0)
add_subdirectory(subdir1)


proj/subdir0/CMakeLists.txt:

add_executable(app0 app0.cpp)
add_executable(app1 app1.cpp)


proj/subdir1/CMakeLists.txt:

add_executable(app2 app2.cpp)


Then in a command prompt simply do:

mkdir <root of proj>/build
cd <root of proj>/build
cmake ..

Upvotes: 13

Related Questions