Is there a good way to make binary files compiled on a high-version machine run on a low-version machine?

152 views
Skip to first unread message

fliter

unread,
Aug 10, 2023, 6:12:23 AM8/10/23
to golang-nuts
I have a project that uses CGO. After compiling on an ubuntu 20.04 compilation machine, it is distributed to other machines for execution, but many of these machines have very low ubuntu versions, which may be ubuntu 16.04 or ubuntu 18.04.

In this way, when executing the binary file, an error similar to version `GLIBC_2.29' not found will be reported.

I try to use -a -ldflags '-extldflags "-static"' when go build. But it seems that because of the use of cgo, it cannot be compiled successfully.

Is there any good way, such as adding appropriate tags when go build, to solve this problem?

Peter Galbavy

unread,
Aug 11, 2023, 3:38:02 AM8/11/23
to golang-nuts
I use a docker container with a centos7 image and build my shared libs there; https://github.com/ITRS-Group/cordial/blob/4f119f7893c67b817baaad23ed75b1ba134bf9c2/Dockerfile#L35

(pls don't judge my Dockerfile skills, it's a learning experience ;) )

Sebastien Binet

unread,
Aug 11, 2023, 4:11:53 AM8/11/23
to golan...@googlegroups.com
Did you try:

export CGO_ENABLED=0
go build

?

fliter

unread,
Aug 12, 2023, 6:17:06 AM8/12/23
to golang-nuts
Thanks. It is indeed a good solution to use containers

fliter

unread,
Aug 12, 2023, 6:17:49 AM8/12/23
to golang-nuts
Because cgo is used, setting CGO_ENABLED=0 will cause problems, and may not compile successfully
Reply all
Reply to author
Forward
0 new messages