Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading variables into cmake from another CMakeLists.txt

Tags:

c++

cmake

Using Ubuntu, C++ and cmake, I want to create a program Alpha which is based upon code for another program,Beta. I already have the source code for Beta, and I need to write code for Alpha which uses this existing code. I am trying to keep everything neat, so I have a separate directory for Alpha and Beta source files, Alpha.cpp and Beta.cpp.

Here is my directory structure:

/CMakeLists.txt
/Alpha.cpp (the main function)
/Beta
    /CMakeLists.txt
    /Beta.cpp

My CMakeLists.txt file for Alpha looks like this:

cmake_minimum_required(VERSION 2.8)
project(Alpha)
add_executable(Alpha Alpha.cpp Beta/Beta.cpp)

Now, in the original code for Beta, the CMakeLists.txt file has lots of information, such as checking the operating system, finding packages, and generally defining various cmake variables. I need to use those variables in the CMakeLists.txt file for Alpha, so that Beta.cpp is compiled properly.

So, my question: is it possible to retain all of this information in Beta/CmakeLists.txt, without having to write it all out in Alpha's CMakeLists.txt file? How do I then tell Alpha's CMakeLists.txt file to read Beta's CMakeLists.txt file to get these variables? I want to do this to keep everything neat, and to maintain modularity. Thanks!

like image 583
Karnivaurus Avatar asked Jan 09 '23 03:01

Karnivaurus


1 Answers

CMake has an include construct which will allow you to pull in other files/modules. http://www.cmake.org/cmake/help/v3.0/command/include.html

So you could separate out whatever you need from Beta/CMakeLists.txt into another file/module called Beta.cmake and include it in Alpha's CMakeLists with include (Beta/Beta.cmake).

So your directory structure would be

/CMakeLists.txt
/Alpha.cpp (the main function)
/Beta
    /CMakeLists.txt
    /Beta.cmake (variables required in Alpha's CMake)
    /Beta.cpp

And the Alpha's CMakeList.txt

cmake_minimum_required(VERSION 2.8)
project(Alpha)
include(Beta/Beta.cmake) (get variables needed from Beta)
add_executable(Alpha Alpha.cpp Beta/Beta.cpp)

Also see How to include an additional CMakeLists.txt

like image 88
ap-osd Avatar answered Jan 17 '23 16:01

ap-osd