I'm having trouble with exporting cmake PROJECT_VERSION_MAJOR
variable to the config.h
file. In my main CMakeLists.txt
I'm setting this variable according to the cmake documentation by invoking project()
in main CMakeLists.txt
file:
cmake_minimum_required(VERSION 3.2.2)
cmake_policy(SET CMP0048 NEW)
set(PROJECT "SampleName")
project(${PROJECT}
VERSION "0.0.0")
configure_file(${CMAKE_SOURCE_DIR}/cmake/config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h)
Through configure_file()
invokation I'm trying to export some cmake variables to the config.h
header file.Please take a look on my config.h.cmake.file
:
#ifndef CONFIG_H
#define CONFIG_H
#cmakedefine PROJECT "@PROJECT@"
#cmakedefine PROJECT_VERSION "@PROJECT_VERSION@"
#cmakedefine PROJECT_VERSION_MAJOR "@PROJECT_VERSION_MAJOR@"
#cmakedefine PROJECT_VERSION_MINOR "@PROJECT_VERSION_MINOR@"
#cmakedefine PROJECT_VERSION_PATCH "@PROJECT_VERSION_PATCH@"
#endif
After running cmake ..
command in my build directory config.h
is created but it look like this:
#ifndef CONFIG_H
#define CONFIG_H
#define PROJECT "SampleName"
#define PROJECT_VERSION "0.0.0"
/* #undef PROJECT_VERSION_MAJOR */
/* #undef PROJECT_VERSION_MINOR */
/* #undef PROJECT_VERSION_PATCH */
#endif
I guess reason of this behaviour is following note in cmake documentation for the configure_file()
function:
Copies an file to an file and substitutes variable values referenced as
@VAR@
or${VAR}
in the input file content. Each variable reference will be replaced with the current value of the variable, or the empty string if the variable is not defined. Furthermore, input lines of the form:
#cmakedefine VAR ..
.will be replaced with either:
#define VAR ...
or:
/* #undef VAR */
depending on whether
VAR
is set in CMake to any value not considered a false constant by theif()
command. The”...”
content on the line after the variable name, if any, is processed as above. Input file lines of the form#cmakedefine01 VAR
will be replaced with either#define VAR 1
or#define VAR 0
similarly.
The question is, can I export cmake PROJECT_VERSION_MAJOR
variable which equals zero ? Or am I doomed for parsing my PROJECT_VERSION
define in my code ?
After applying advice from hank, newly generated file looks like this:
#ifndef CONFIG_H
#define CONFIG_H
#define PROJECT "SampleName"
#define PROJECT_VERSION "0.0.0"
#define PROJECT_VERSION_MAJOR "0"
#define PROJECT_VERSION_MINOR "0"
#define PROJECT_VERSION_PATCH "0"
#endif
I think in your case you should not use #cmakedefine
directive. Use simple #define
instead:
#ifndef CONFIG_H
#define CONFIG_H
#define PROJECT "@PROJECT@"
#define PROJECT_VERSION "@PROJECT_VERSION@"
#define PROJECT_VERSION_MAJOR "@PROJECT_VERSION_MAJOR@"
#define PROJECT_VERSION_MINOR "@PROJECT_VERSION_MINOR@"
#define PROJECT_VERSION_PATCH "@PROJECT_VERSION_PATCH@"
#endif
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With