将版本和架构信息插入 C++ 的最佳方法是什么?来源?
我希望我的 C++ 程序包含一个“--version”选项,使其打印出:
- Architecture it's Compiled for
- Version of the source (eg, v0.1.0)
- Name of the application
我也使用 autoconf/automake 作为第一次,我注意到configure.ac既有二进制文件又有版本。目前它没有架构信息,我不想添加此类信息,因为我将在多个架构下进行编译。
有没有一种简单的方法可以自动将版本/架构/应用程序名称信息插入头文件或源文件中?大多数 C++ 程序员是如何做到这一点的?
I want my C++ program to include a "--version" option which causes it to print out:
- Architecture it's compiled for
- Version of the source (e.g., v0.1.0)
- Name of the application
I'm also using autoconf/automake for the first time, and I notice that configure.ac has both the binary and the version. It doesn't currently have architecture information in it, and I don't want to add such info, since I'll be compiling under multiple arches.
Is there an easy way to automate the insertion of the version/arch/appname information in a header or source file? How do most C++ coders do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
autoconf 为您提供了一个
config.h
,其中提供了字符串宏,例如PACKAGE
、PACKAGE_NAME
、PACKAGE_STRING
、PACKAGE_VERSION
和VERSION
。(当我使用 Argp 参数解析器时,我只需使用
然后
--help
、--version
等会自动工作。)您还可以添加
到您的
configure.ac
,如果您也希望在您的 config.h 中包含这些字符串宏。这些通常是$arch-$vendor-$kernel-$libc
形式的字符串,其中CHOST
是软件构建后运行的平台,CBUILD
是当前正在构建软件的平台,并且CTARGET
是软件将运行的平台。(除非您正在交叉编译或构建交叉编译工具链,否则这些都是相同的。)
autoconf gives you a
config.h
, which provides string macros such asPACKAGE
,PACKAGE_NAME
,PACKAGE_STRING
,PACKAGE_VERSION
, andVERSION
.(When I use the Argp argument parser, I simply use
and then
--help
,--version
, etc. just automatically work.)You could also add
to your
configure.ac
, if you want to have those string macros in yourconfig.h
too. These are typically strings of the form$arch-$vendor-$kernel-$libc
, whereCHOST
is the platform that will run the software after it is built,CBUILD
is the platform that is currently building the software, andCTARGET
is the platform the software will act on.(These are all the same unless you are cross-compiling or building a cross-compile toolchain.)
编写一个生成版本源文件的 python 脚本。然后让它通过您正在使用的任何构建脚本/工具运行。
选择一条规则来实际重写版本文件并在 python 脚本中强制执行它:它实际上取决于您正在执行的项目类型和发布组织。
在我的家庭项目中,我使用一个脚本在每次构建时生成一个版本文件,因为我需要知道哪个计算机开发人员在哪个时间构建了该版本。
但在很多工作中,我使用了一个生成版本文件的脚本,但仅在我想要的情况下才使用。这是因为开发团队决定特定版本何时是“发布”。
我建议使用 Python 脚本,因为 Python 可以在所有开发平台上运行,并且在文本文件操作方面非常强大且简单。然后它可以由您使用的任何构建系统(Visual Studio、CMake 等)调用。
Write a python script that generate your version source file. Then make it run by whatever build scripts/tool you're using.
Choose a rule to actually rewrite the version file and enforce it in the python script : it's really dependent on the type of project and release organisation you're doing.
In my home project I use a script that generate a version file each time I build, because I need to know wich computer-developer built the version and at wich time.
But in a lot of job works I used a script that generated the version file but only if I wanted it to. That's because the dev team decided when a specific version was a "release".
I suggested using a Python script because Python runs on all developement platforms and is really powerful and easy when it comes to text files manipulations. It can then be called by whatever build system (Visual Studio, CMake, etc.) you use.