Go build with another glibc
Asked Answered
B

3

19

I have installed another version of GLIBC and want to compile Golang code against this new GLIBC.

I have tried the following command for dynamic compilation:

go build --ldflags '-linkmode external -L /path/to/another_glibc/

But when I run ldd "go_executable", it still shows linked to default glibc.

Output:

linux-vdso.so.1 =>  (0x00007fff29da7000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f128a93c000)
/lib64/ld-linux-x86-64.so.2 (0x00007f128ad06000)

Expected Output:

linux-vdso.so.1 =>  (0x00007fff45fa7000)
libc.so.6 => /another_glibc/lib/libc.so.6 (0x00007f5cd2067000)
/another_glibc/ld-2.29.so => /lib64/ld-linux-x86-64.so.2 (0x00007f5cd2420000)

What is missing here?

Birdella answered 1/4, 2019 at 7:31 Comment(0)
B
9

Before doing go build Set

CGO_LDFLAGS

Dynamic:

export CGO_LDFLAGS="-Xlinker -rpath=/path/to/another_glibc/lib"

Static:

export CGO_LDFLAGS="-Xlinker -rpath=/path/to/another_glibc/lib -static"

CGO_LDFLAGS lets you set GCC-like ld flags for Go.

Birdella answered 1/4, 2019 at 12:44 Comment(0)
S
37

This is not an answer to the question, just a warning:

If you, like me, came here because you were compiling to deploy on another machine and got "version `GLIBC_2.32' not found" (or similar), but you were not intentionally using CGo, stop here.

Go on Linux dynamically links C libraries to have faster and smaller builds, but it is able to supplement them for example when cross-compiling.

You can do export CGO_ENABLED=0 to disable CGo and get rid of the dependencies.

Subgroup answered 6/6, 2022 at 12:57 Comment(0)
B
9

Before doing go build Set

CGO_LDFLAGS

Dynamic:

export CGO_LDFLAGS="-Xlinker -rpath=/path/to/another_glibc/lib"

Static:

export CGO_LDFLAGS="-Xlinker -rpath=/path/to/another_glibc/lib -static"

CGO_LDFLAGS lets you set GCC-like ld flags for Go.

Birdella answered 1/4, 2019 at 12:44 Comment(0)
P
3

bitbyter's answer is not correct for the dynamic case because it requires that the system dynamic linker is compatible with the non-system glibc, which is unlikely. You can set the dynamic linker like this:

export CGO_LDFLAGS="-Xlinker -rpath=/path/to/another_glibc/lib64"
CGO_LDFLAGS="$CGO_LDFLAGS -Xlinker --dynamic-linker="/path/to/another_glibc/lib64/ld-linux-x86-64.so.2"

The dynamic linker name is specific to the architecture, so you have to research its name.

Papuan answered 15/5, 2020 at 4:57 Comment(1)
I did not face the dynamic linker incompatibility issue, hence didn't think further. Thanks for the next step to try out in case the command in my answer doesn't work.Birdella

© 2022 - 2024 — McMap. All rights reserved.