将版本和架构信息插入 C++ 的最佳方法是什么?来源?

发布于 2024-08-19 04:38:20 字数 346 浏览 4 评论 0原文

我希望我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

望笑 2024-08-26 04:38:20

autoconf 为您提供了一个 config.h,其中提供了字符串宏,例如 PACKAGEPACKAGE_NAMEPACKAGE_STRINGPACKAGE_VERSIONVERSION

(当我使用 Argp 参数解析器时,我只需使用

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <argp.h>
const char *argp_program_version = PACKAGE_STRING;
const char *argp_program_bug_address = PACKAGE_BUGREPORT;
/* et cetera */
int main(int argc, char **argv) {
    error_t argp_result = argp_parse(&argp, argc, argv, 0, 0, NULL);
    if (argp_result) return argp_result;
    /* et cetera */
}

然后 --help--version 等会自动工作。)

您还可以添加

AC_CANONICAL_HOST
AC_DEFINE_UNQUOTED([CHOST], ["$host"], [Canonical host])
AC_CANONICAL_BUILD
AC_DEFINE_UNQUOTED([CBUILD], ["$build"], [Canonical build])
AC_CANONICAL_TARGET
AC_DEFINE_UNQUOTED([CTARGET], ["$target"], [Canonical target])

到您的 configure.ac,如果您也希望在您的 config.h 中包含这些字符串宏。这些通常是 $arch-$vendor-$kernel-$libc 形式的字符串,其中
CHOST 是软件构建后运行的平台,
CBUILD 是当前正在构建软件的平台,并且
CTARGET 是软件将运行的平台。
(除非您正在交叉编译或构建交叉编译工具链,否则这些都是相同的。)

autoconf gives you a config.h, which provides string macros such as PACKAGE, PACKAGE_NAME, PACKAGE_STRING, PACKAGE_VERSION, and VERSION.

(When I use the Argp argument parser, I simply use

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <argp.h>
const char *argp_program_version = PACKAGE_STRING;
const char *argp_program_bug_address = PACKAGE_BUGREPORT;
/* et cetera */
int main(int argc, char **argv) {
    error_t argp_result = argp_parse(&argp, argc, argv, 0, 0, NULL);
    if (argp_result) return argp_result;
    /* et cetera */
}

and then --help, --version, etc. just automatically work.)

You could also add

AC_CANONICAL_HOST
AC_DEFINE_UNQUOTED([CHOST], ["$host"], [Canonical host])
AC_CANONICAL_BUILD
AC_DEFINE_UNQUOTED([CBUILD], ["$build"], [Canonical build])
AC_CANONICAL_TARGET
AC_DEFINE_UNQUOTED([CTARGET], ["$target"], [Canonical target])

to your configure.ac, if you want to have those string macros in your config.h too. These are typically strings of the form $arch-$vendor-$kernel-$libc, where
CHOST is the platform that will run the software after it is built,
CBUILD is the platform that is currently building the software, and
CTARGET is the platform the software will act on.
(These are all the same unless you are cross-compiling or building a cross-compile toolchain.)

彼岸花似海 2024-08-26 04:38:20

编写一个生成版本源文件的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文