英文:
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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论