export CMake variable to C++
Solved
General and Desktop
-
Inside my CMakeLists.txt I wrote
project(CMake_variable_to_code LANGUAGES CXX C DESCRIPTION "export CMake variable to C++ code example" VERSION 1.0.0.0)
How I can get CMake variables in C++ code? I want to get values: "CMake_variable_to_code", "export CMake variable to C++ code example", "1.0.0.0". And I also make suggestion to Qt to export variables from CMakeLists.txt
-
I make code example for both ways. Here
-
This is export CMake variable to C++ code example. I also use Qt but it is not a main point. In breaf
- Inside CMakeLists.txt any project has something like
project(CMake_variable_to_code LANGUAGES CXX C DESCRIPTION "export CMake variable to C++ code example" VERSION 1.0.0.0)
- Somewhere inside CMakeLists.txt you should add
add_compile_definitions(MYPROJECT_NAME=${PROJECT_NAME}) add_compile_definitions(MYPROJECT_DESCRIPTION=${PROJECT_DESCRIPTION}) add_compile_definitions(CMAKE_PROJECT_VERSION=${CMAKE_PROJECT_VERSION})
- Then in C++ code you should add
#define Q(x) #x #define QUOTE(x) Q(x)
- Now you can use CMake's variables inside C++ code. For example
QCoreApplication::setApplicationName(QUOTE(MYPROJECT_NAME)); QCoreApplication::setApplicationVersion(QUOTE(CMAKE_PROJECT_VERSION));
See also my feature request for Qt project.
Compiling
git clone https://github.com/AndreiCherniaev/CMake_variable_to_code.git && cd CMake_variable_to_code rm -rf build-host && mkdir build-host/ cmake -S src/ -B build-host/ cmake --build build-host/ --parallel
Test
build-host/CMake_variable_to_code --version CMake_variable_to_code 1.0.0.0
build-host/CMake_variable_to_code --help Usage: build-host/CMake_variable_to_code [options] export CMake variable to C++ code example Options: -h, --help Displays help on commandline options. --help-all Displays help, including generic Qt options. -v, --version Displays version information.
See full-code repo.
-
-
Here is a different approach from the official CMake tutorial: https://cmake.org/cmake/help/latest/guide/tutorial/A Basic Starting Point.html#exercise-3-adding-a-version-number-and-configured-header-file
-
-
I make code example for both ways. Here
-