'CMake not defining variables

When trying to define project version variables in a config.h for inclusion in source files, CMake doesn't define them

CMakeLists.txt looks like this

cmake_minimum_required(VERSION 3.0)
project(podder VERSION 1.0 LANGUAGES CXX)

configure_file(config.h.in config.h ESCAPE_QUOTES)

config.h.in

#ifndef HAVE_CONFIG_H
#define HAVE_CONFIG_H

#cmakedefine ${CMAKE_PROJECT_VERSION}

#endif

And the generated config.h looks like this

#ifndef HAVE_CONFIG_H
#define HAVE_CONFIG_H

/* #undef  */

#endif


Solution 1:[1]

What you want is

#ifndef HAVE_CONFIG_H
#define HAVE_CONFIG_H

#cmakedefine PROJECT_VERSION ${PROJECT_VERSION}

#endif

The syntax is #cmakedefine VAR ... where VARis the variable name (without $()) and the expression ... is evaluated, see configure_file(). If the variable VAR is not defined or evaluates to false it is replaced by /* #undef VAR */.

Solution 2:[2]

You should use#define @CMAKE_PROJECT_VERSION@ instead of #cmakedefine ${CMAKE_PROJECT_VERSION}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 havogt
Solution 2