无法导出 cmake PROJECT_VERSION_MAJOR 因为它等于零
Cannot export cmake PROJECT_VERSION_MAJOR because it equals zero
我在将 cmake PROJECT_VERSION_MAJOR
变量导出到 config.h
文件时遇到问题。在我的主 CMakeLists.txt
中,我根据 cmake 文档通过在主 CMakeLists.txt
文件中调用 project()
来设置此变量:
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)
通过 configure_file()
调用,我试图将一些 cmake 变量导出到 config.h
header file.Please 看看我的 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
在我的构建目录 config.h
中创建 运行 cmake ..
命令后,它看起来像这样:
#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
我猜这种行为的原因是 configure_file()
函数的 cmake 文档中的以下注释:
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 the if()
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.
问题是,我可以导出等于零的 cmake PROJECT_VERSION_MAJOR
变量吗?还是我注定要解析我在代码中定义的 PROJECT_VERSION
?
应用 hank 的建议后,新生成的文件如下所示:
#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
我认为在你的情况下你不应该使用 #cmakedefine
指令。使用简单的 #define
代替:
#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
我在将 cmake PROJECT_VERSION_MAJOR
变量导出到 config.h
文件时遇到问题。在我的主 CMakeLists.txt
中,我根据 cmake 文档通过在主 CMakeLists.txt
文件中调用 project()
来设置此变量:
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)
通过 configure_file()
调用,我试图将一些 cmake 变量导出到 config.h
header file.Please 看看我的 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
在我的构建目录 config.h
中创建 运行 cmake ..
命令后,它看起来像这样:
#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
我猜这种行为的原因是 configure_file()
函数的 cmake 文档中的以下注释:
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.
问题是,我可以导出等于零的 cmake PROJECT_VERSION_MAJOR
变量吗?还是我注定要解析我在代码中定义的 PROJECT_VERSION
?
应用 hank 的建议后,新生成的文件如下所示:
#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
我认为在你的情况下你不应该使用 #cmakedefine
指令。使用简单的 #define
代替:
#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