Skip to content

Commit

Permalink
build: Remove -extldflags "-static" from LDFLAGS
Browse files Browse the repository at this point in the history
Using this flag is actually misleading, as it's ineffective unless
'-linkmode external' is used.
With the current LDFLAGS, whether we get a static build or not is highly
dependent on the arch, on the code being compiled, on the use of cgo or
not, ... but there are no guarantees at all that the build will be
static.

$ go build -ldflags '-s -w -extldflags "-static"' -o routes-controller .
$ ldd routes-controller
linux-vdso.so.1 (0x00007ffd2dcc4000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fb2a4888000)
libc.so.6 => /lib64/libc.so.6 (0x00007fb2a46b9000)
/lib64/ld-linux-x86-64.so.2 (0x00007fb2a48bf000)

$ CGO_ENABLED=0 go build -ldflags '-s -w -extldflags "-static"' -o routes-controller .
$ ldd routes-controller
not a dynamic executable

$ CGO_ENABLED=0 go build -ldflags '-s -w' -o routes-controller .
$ ldd routes-controller
not a dynamic executable

On my machine, the `crc` binary generated by `make cross` dynamically
links to libc regardless of the presence or not of this flag.

Moreover, static linking would require installation of glibc-static on
some arches, but statically linking against glibc is not supported on
some OSes (RHEL8).

This commit removes the `-extldflags "-static"` LDFLAGS from Makefile.
The binaries are unchanged before/after that change (same size, and very
few content differences)
  • Loading branch information
cfergeau authored and guillaumerose committed Aug 17, 2021
1 parent 19538da commit cc42d69
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ ifdef OKD_VERSION
endif

# https://golang.org/cmd/link/
LDFLAGS := $(VERSION_VARIABLES) -extldflags='-static' ${GO_EXTRA_LDFLAGS}
LDFLAGS := $(VERSION_VARIABLES) ${GO_EXTRA_LDFLAGS}

# Add default target
.PHONY: default
Expand Down

0 comments on commit cc42d69

Please sign in to comment.