configure_file() does not generate H file as expected (PROJECT_VERSION_MAJOR, <Project>_VERSION_MINOR etc.)
Hi!
I looked over tutorials of CMake and stumbled about generation of H file described in the Exercise 3. Using released version 3.27.7 (or even the RC 3.28.0-rc1), it does not work as expected and described in the tutorial, documentation is also not giving a clear answer.
The problem
You can define version of project, but generator fails extracting and substituting version component values like PROJECT_VERSION_MAJOR
, <Project>_VERSION_MINOR
etc. if the corresponding version component in project()
was defined as zero.
Example (based on tutorial)
CMakeLists.txt
project(Tutorial VERSION 0.2.3 DESCRIPTION "CMake Tutorial")
configure_file(TutorialConfig.h.in TutorialConfig.h @ONLY)
Input:
TutorialConfig.h.in
#cmakedefine Tutorial_VERSION "@PROJECT_VERSION@"
#cmakedefine Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#cmakedefine Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
#cmakedefine Tutorial_VERSION_PATCH @Tutorial_VERSION_PATCH@
#cmakedefine PROJECT_VERSION "@Tutorial_VERSION@"
#cmakedefine PROJECT_VERSION_MAJOR @PROJECT_VERSION_MAJOR@
#cmakedefine PROJECT_VERSION_MINOR @PROJECT_VERSION_MINOR@
#cmakedefine PROJECT_VERSION_PATCH @PROJECT_VERSION_PATCH@
Output:
TutorialConfig.h
#define Tutorial_VERSION "0.2.3"
/* #undef Tutorial_VERSION_MAJOR */
#define Tutorial_VERSION_MINOR 2
#define Tutorial_VERSION_PATCH 3
#define PROJECT_VERSION "0.2.3"
/* #undef PROJECT_VERSION_MAJOR */
#define PROJECT_VERSION_MINOR 2
#define PROJECT_VERSION_PATCH 3
Result:
Consuming application (in the case tutorial.cxx) fails to compile, you should have to add to your source code #ifndef / #define
to workaround the issue.
Notes:
- In the tutorial there was provided an example with the version 1.0
- both
PROJECT_VERSION
and<Project>_VERSION
work as expected - if used
#define
in the input template file (e.g.#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
), then it works as expected too.
Best Regards, Aivaras P.