Issue
I want to create a second file, for example Instructions.txt, and be able to include it's CMake instructions to the main CMakeLists.txt, for example:
// Instructions.txt
set(MY_VARIABLE value)
set(MY_SECOND_VARIABLE 1234)
// CMakeLists.txt
<HERE INCLUDE INSTRUCTIONS.TXT>
// And be able to use those variables, for example
project(MY_VARIABLE)
Solution
You usually do that simply by using another cmake.
Contrary to what you might believe, the file extension is different for other file than build files.
Let's call this file instructions.cmake
:
set(MY_VARIABLE value)
set(MY_SECOND_VARIABLE 1234)
Then you can include it like this inside your CMakeLists.txt
:
include(instructions.cmake)
If the file is in the module path, you can include it like that:
include(instructions)
There are some other filename that are important for CMake though.
For example, any file that start with Find
then ends with .cmake
like FindXYZ.cmake
, the XYZ
part is assumed to be a package name and in that case you should use find_package(XYZ REQUIRED)
.
Answered By - Guillaume Racicot Answer Checked By - Mary Flores (WPSolving Volunteer)