CMake:从文件中读取内部版本号以设置变量 [英] CMake: Read build number from file to set a variable

查看:847
本文介绍了CMake:从文件中读取内部版本号以设置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目中,内部版本号存储在项目根目录下名为"BuildNumber.txt"的文件中.我想做的就是让CMake从该文件中读取数字并设置一个可以应用于头文件的变量.

setup.h.in

#define build_number "@BUILD_NUMBER@";

使用configure_file,可以使用CMake变量替换上述.in文件中的占位符.是否有可能让CMake将BuildNumber.txt中的数字读入变量中?

解决方案

您可以为此目的使用CMake命令file (STRINGS ...).假设内部版本号位于文件BuildNumber.txt的一行中,以下命令会将其读取到CMake变量BUILD_NUMBER中:

file (STRINGS "BuildNumber.txt" BUILD_NUMBER)

另请参见文件命令参考./p>

I'm working on a project where the build number is stored in a file called 'BuildNumber.txt' at the root of the project. What I'd like to do is have CMake read the number from this file and set a variable that can be applied to a header file.

setup.h.in

#define build_number "@BUILD_NUMBER@";

Using configure_file, it's possible to replace placeholders in an .in file like above with a CMake variable. Is it possible to get CMake to read in the number from BuildNumber.txt into a variable?

解决方案

You can use the CMake command file (STRINGS ...) for that purpose. Assuming the build number is located in the file BuildNumber.txt in a single line, the following command will read it into the CMake variable BUILD_NUMBER:

file (STRINGS "BuildNumber.txt" BUILD_NUMBER)

Also see the file command reference.

这篇关于CMake:从文件中读取内部版本号以设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆