如何将版本信息嵌入到共享库和二进制文件中?

发布于 2024-09-11 05:32:09 字数 168 浏览 1 评论 0原文

在 Linux 上,有没有办法将版本信息嵌入到 ELF 二进制文件中?我想在编译时嵌入此信息,以便稍后可以使用脚本将其提取。一种黑客方法是植入可以使用 strings 命令提取的东西。是否有更传统的方法,类似于 Visual Studio 如何为 Windows DLL 生成版本信息(注意 DLL 属性中的版本选项卡)?

On Linux, is there a way to embed version information into an ELF binary? I would like to embed this info at compile time so it can then be extract it using a script later. A hackish way would be to plant something that can be extracted using the strings command. Is there a more conventional method, similar to how Visual Studio plant version info for Windows DLLs (note version tab in DLL properties)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

紙鸢 2024-09-18 05:32:09

如果使用 cvs 或 subversion,一种方法是在源文件中使用专门格式化的特殊 id 字符串。然后向 cvs 或 svn 添加一个预提交挂钩,以便在提交更改时使用文件的新版本更新该特殊变量。然后,当构建二进制文件时,您可以使用 ident 来提取该信息。例如:

将类似这样的内容添加到您的 cpp 文件中:

static char fileid[] = "$Id: fname.cc,v 1.124 2010/07/21 06:38:45 author Exp $";

在程序上运行 ident (您可以通过安装 rcs 找到它)应该显示有关其中包含 id 字符串的文件的信息。

ident program
program:
    $Id: fname.cc,v 1.124 2010/07/21 06:38:45 author Exp $

注意 正如人们在评论中提到的那样,这种技术已经过时了。让源代码控制系统自动更改源代码是很丑陋的,而且自从 cvs 成为唯一选择以来,源代码控制已经得到了改进,这意味着您可以找到更好的方法来实现相同的目标。

One way to do it if using cvs or subversion is to have a special id string formatted specially in your source file. Then add a pre-commit hook to cvs or svn that updates that special variable with the new version of the file when a change is committed. Then, when the binary is built, you can use ident to extract that indformation. For example:

Add something like this to your cpp file:

static char fileid[] = "$Id: fname.cc,v 1.124 2010/07/21 06:38:45 author Exp $";

And running ident (which you can find by installing rcs) on the program should show the info about the files that have an id string in them.

ident program
program:
    $Id: fname.cc,v 1.124 2010/07/21 06:38:45 author Exp $

Note As people have mentioned in the comments this technique is archaic. Having the source control system automatically change your source code is ugly and the fact that source control has improved since the days when cvs was the only option means that you can find a better way to achieve the same goals.

骄兵必败 2024-09-18 05:32:09

为了扩展@sashan的答案,同时避免@cdunn2001提到的“$Id:$”问题,...

您可以将文件“version_info.h”添加到您的项目中,该文件仅具有:

#define VERSION_MAJOR "1"
#define VERSION_MINOR "0"
#define VERSION_PATCH "0"
#define VERSION_BUILD "0"

并且在您的main.c文件中该行:(

static char version[] = VERSION_MAJOR "." VERSION_MINOR "." VERSION_PATCH "." VERSION_BUILD;
static char timestamp[] = __DATE__ " " __TIME__;

或者您希望在程序中使用这些值)

然后设置一个预构建步骤,该步骤读取 version_info.h 文件,适当地增加数字,然后再次将其写回。每日构建只会增加 VERSION_BUILD 数量,而更严重的版本会增加其他数字。

如果你的 makefile 在你的对象的先决条件列表中列出了这个,那么构建将重新编译它需要的内容。

To extend the @sashang answer, while avoiding the "$Id:$" issues mentioned by @cdunn2001, ...

You can add a file "version_info.h" to your project that has only:

#define VERSION_MAJOR "1"
#define VERSION_MINOR "0"
#define VERSION_PATCH "0"
#define VERSION_BUILD "0"

And in your main.c file have the line:

static char version[] = VERSION_MAJOR "." VERSION_MINOR "." VERSION_PATCH "." VERSION_BUILD;
static char timestamp[] = __DATE__ " " __TIME__;

(or however you want to use these values in your program)

Then set up a pre-build step which reads the version_info.h file, bumps the numbers appropriately, and writes it back out again. A daily build would just bump the VERSION_BUILD number, while a more serious release would bump other numbers.

If your makefile lists this on your object's prerequisite list, then the build will recompile what it needs to.

你与清晨阳光 2024-09-18 05:32:09

英特尔 Fortran 和 C++ 编译器当然可以使用 -sox 选项来做到这一点。所以,是的,有办法。我不知道在二进制文件中嵌入此类信息有什么广泛的约定,我通常使用十六进制模式的 Emacs 来读取嵌入的信息,这是相当 hackish 的。

“-sox”还嵌入了用于构建可执行文件的编译器选项,这非常有用。

The Intel Fortran and C++ compilers can certainly do this, use the -sox option. So, yes there is a way. I don't know of any widespread convention for embedding such information in a binary and I generally use Emacs in hexl-mode for reading the embedded information, which is quite hackish.

'-sox' also embeds the compiler options used to build an executable, which is very useful.

鸵鸟症 2024-09-18 05:32:09

如果您声明一个名为 program_version 或类似的变量,您可以找出该变量存储在哪个地址,然后继续提取其值。例如,

objdump -t --demangle /tmp/libtest.so | grep program_version
0000000000600a24 g     O .data  0000000000000004              program_version

告诉我 program_version 位于地址 0000000000600a24 处,大小为 4。然后只需读取文件中该地址处的值即可。

或者您可以编写一个简单的程序来链接问题中的库并打印版本,定义为导出变量或函数。

If you declare a variable called program_version or similar you can find out at which address the variable is stored at and then proceed to extract its value. E.g.

objdump -t --demangle /tmp/libtest.so | grep program_version
0000000000600a24 g     O .data  0000000000000004              program_version

tells me that program_version resides at address 0000000000600a24 and is of size 4. Then just read the value at that address in the file.

Or you could just write a simple program that links the library in questions and prints the version, defined either as an exported variable or a function.

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