cmake没有定义变量

CMake not defining variables

本文关键字:变量 定义 cmake      更新时间:2023-10-16

试图在config.h中定义项目版本变量以包含在源文件中时,cmake并未定义它们

cmakelists.txt看起来像

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

和生成的config.h看起来像

#ifndef HAVE_CONFIG_H
#define HAVE_CONFIG_H
/* #undef  */
#endif

您想要的是

#ifndef HAVE_CONFIG_H
#define HAVE_CONFIG_H
#cmakedefine PROJECT_VERSION ${PROJECT_VERSION}
#endif

语法是 #cmakedefine VAR ...,其中 VAR是变量名称(没有 $()(,并且评估了表达式 ...,请参见configure_file((。如果未定义变量VAR或评估为false,则由/* #undef VAR */替换。