如何从一个Go库中获取对象构建信息?

huangapple go评论94阅读模式
英文:

How do I get object build info out of a Go library?

问题

我正在尝试构建一个Go程序,在我的系统上有一个以二进制形式存在的必需库。然而,go build失败了,显示错误信息为:

object is [linux amd64 go1.1.1 X:none] expected [linux amd64 go1.1.2 X:none]

我看到了直接的问题所在:静态库是使用较旧版本的Go构建的。我如何直接从.a文件中读取该信息?(我可以使用strings library.a | grep '^go object'查看,但是否有一种方法可以输出构建字符串?(这个字符串应该如何正确称呼?)

英文:

I'm trying to build a Go program where I have a required library on my system in binary form. However, go build fails with

object is [linux amd64 go1.1.1 X:none] expected [linux amd64 go1.1.2 X:none]

I see what the immediate problem is: the static library was built with an older version of Go. How can I read that information from the .a file directly? (I can see it with strings library.a | grep '^go object', but is there something that's meant to output the build string? (And, what is that string properly called?)

答案1

得分: 1

.a文件是Go编译器生成的,使用pack工具进行管理。用于链接包的元数据可以在存档的__.PKGDEF成员中找到。

您可以使用以下命令将此文件从存档中提取到stdout

go tool pack p path/to/package.a __.PKGDEF

它以您所需的构建签名开头,因此您可以选择获取第一行或使用^go object进行grep,就像您使用strings的输出一样(这应该更可靠,以防该文本出现为程序代码中的常量)。

英文:

The .a files the Go compiler produces are managed using the pack tool. The metadata used to link the package is found in the __.PKGDEF member of the archive.

You can extract this file from an archive to stdout with:

go tool pack p path/to/package.a __.PKGDEF

It starts with the build signature you're after, so you could either take the first line or grep for ^go object as you were with the output of strings (this should be a bit more reliable, in case that text shows up as a constant in the program code).

huangapple
  • 本文由 发表于 2013年9月22日 11:26:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/18939966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定