如何构建 CGO 程序,静态链接到 glibc,并动态链接到 libGL.so?

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

How to build CGO program static linking to glibc and dynamic linking to libGL.so?

问题

我的操作系统是Kali,运行的GLIBC_2.32版本。我需要为运行GLIBC_2.28版本的Debian 10系统构建一个CGO应用程序。

如果我使用动态链接进行go build,它无法在Debian系统上运行,显示GLIBC不匹配的错误:

找不到版本`GLIBC_2.29`
找不到版本`GLIBCXX_3.4.29`
找不到版本`GLIBC_2.32`

所以我尝试了静态链接:CGO_LDFLAGS='-static' go build。一个GUI库使用了OpenGL,它显示错误:

# github.com/go-gl/gl/v3.2-core/gl
/usr/bin/ld: 找不到 -lGL

在搜索了一段时间后,我发现libGL与GPU驱动程序有关,无法进行静态链接。

然后我尝试通过以下方式动态链接libGL.so,并静态链接其他库:
CGO_LDFLAGS='-L/usr/lib/x86_64-linux-gnu -Bdynamic -lGL -static' go build

但是仍然出现相同的错误:"找不到 -lGL"

我不想使用Docker,它太重了。而且我不认为从Debian 10升级到11会解决问题,因为将来可能会有其他客户端运行不同的操作系统。有什么最佳解决方案?

英文:

My os is Kali, running GLIBC_2.32. I need to build an CGO application for a debian 10 system, which is running GLIBC_2.28.

If I go build with dynamic linking, it can't be run on the debian system, it shows GLIBC mismatch:

version `GLIBC_2.29` not found
version `GLIBCXX_3.4.29` not found
version `GLIBC_2.32` not found

So I tried static linking: CGO_LDFLAGS='-static' go build. A gui library uses OpenGL and it shows error:

# github.com/go-gl/gl/v3.2-core/gl
/usr/bin/ld: cannot find -lGL

After searching a while I found the libGL is related to the gpu driver and can't be statically linked.

Then I tried linking libGL.so dynamically and statically linking other libraries by:

CGO_LDFLAGS='-L/usr/lib/x86_64-linux-gnu -Bdynamic -lGL -static' go build

But same error: "cannot find -lGL"

I don't want to use docker, it's too heavy. And I don't think upgrading from debian 10 to 11 solves the problem, there maybe some other clients running different os in the future. What's the best solution?

答案1

得分: 3

然后我尝试通过以下方式动态链接libGL.so并静态链接其他库:
CGO_LDFLAGS='-L/usr/lib/x86_64-linux-gnu -Bdynamic -lGL -static' go build

-static标志告诉链接器执行一个完全静态链接。无论你将它放在-lGL之前还是之后,意义是相同的。

要静态链接一些库并动态链接其他库,请参考这个答案

话虽如此,你所尝试的做法是不可能的:如果你的链接中有任何动态库,那么libc.so.6也必须是动态链接的。

我不想使用Docker,它太重了。

很遗憾,你必须使用Docker,或者设置一个chroot环境,或者构建一个“Linux到旧版Linux”的交叉编译器。使用Docker可能是最容易实现的方法。

英文:

> Then I tried linking libGL.so dynamically and statically linking other libraries by:
> CGO_LDFLAGS='-L/usr/lib/x86_64-linux-gnu -Bdynamic -lGL -static' go build

The -static flag tells the linker: perform a completely static link. It doesn't matter whether you put it before or after -lGL, the meaning is the same.

To link some libraries statically and link others dynamically, see this answer.

That said, what you are trying to do is impossible: if you have any dynamic libraries in your link, then libc.so.6 must also be dynamically linked.

> I don't want to use docker, it's too heavy.

Too bad. You'll have to use docker, or set up a chroot environment, or build yourself a "Linux to older Linux" crosscompiler. The docker is likely easiest to implement.

huangapple
  • 本文由 发表于 2021年12月22日 04:00:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/70440984.html
匿名

发表评论

匿名网友

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

确定