Reading variables in cmake from another CMakeLists.txt

Using Ubuntu, C++and cmakeI want to create a program Alphathat is based on the code for other programs Beta. I already have the source code for Beta, and I need to write code for Alphathat uses this existing code. I try to keep everything in order, so I have a separate directory for Alphaboth the Betasource files Alpha.cppand Beta.cpp.

Here is my directory structure:

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

My CMakeLists.txtfile for Alphalooks like this:

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

Now, in the source code for Beta, the file CMakeLists.txtcontains a lot of information, such as checking the operating system, searching for packages and, as a rule, defining various cmake variables. I need to use these variables in the file CMakeLists.txtfor Alpha, so that Beta.cppcompiled correctly.

So my question is: is it possible to save all this information in Beta/CmakeLists.txtwithout writing it all to a file Alpha CMakeLists.txt? How do I transfer a file Alpha CMakeLists.txtto read a file Beta CMakeLists.txtto get these variables? I want to do this in order to keep everything neat and maintain modularity. Thank!

+4
source share
1 answer

CMake include, /. http://www.cmake.org/cmake/help/v3.0/command/include.html

, , , Beta/CMakeLists.txt / Beta.cmake Alpha CMakeLists include (Beta/Beta.cmake).

,

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

Alpha 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)

. CMakeLists.txt

+7

Source: https://habr.com/ru/post/1570293/


All Articles