CGO link whole library on mac


I’m using library as .a file and which need to be whole imported during linked process.

Unfortunately the flag -Wl,-all_load doesn’t work on MacOS and the flag -Wl,--whole-archive doesn’t work on Linux:

#cgo darwin LDFLAGS: -L.. -Wl,-all_load -lmy_lib

go build _/Users/thierryfournier/git/my-lib/my_lib: invalid flag in #cgo LDFLAGS: -Wl,-all_load

nor the whole archive on Linux:

#cgo LDFLAGS: -L.. -Wl,--whole-archive -lmy_lib

go build _/home/thierry/my_lib/my_lib: invalid flag in #cgo LDFLAGS: -Wl,--whole-archive

So, i don’t found any way to embed the whole library.

  • I don’t find a way to understand how the linker is called
  • I don’t find documentation about supported flag.

If anyone has a solution or a clue, it will be welcome


1 Like

What does “doesn’t work” mean? Are you getting an error?

Yes, the error is in the post:

cgo does not appear to recognize suboption commas.

Post your question on golang-nuts.

Thanks. I’ll try

Using Ian’s suggestion,
Add these flags to the CGO_LDFLAGS_ALLOW environment variable.

Command cgo

Using cgo with the go command

For security reasons, only a limited set of flags are allowed, notably -D, -U, -I, and -l. To allow additional flags, set CGO_CFLAGS_ALLOW to a regular expression matching the new flags. To disallow flags that would otherwise be allowed, set CGO_CFLAGS_DISALLOW to a regular expression matching arguments that must be disallowed. In both cases the regular expression must match a full argument: to allow -mfoo=bar, use CGO_CFLAGS_ALLOW=’-mfoo.*’, not just CGO_CFLAGS_ALLOW=’-mfoo’. Similarly named variables control the allowed CPPFLAGS, CXXFLAGS, FFLAGS, and LDFLAGS.

for Linux,

#cgo linux LDFLAGS: -L.. -Wl,--whole-archive -lmy_lib -Wl,--no-whole-archive

CGO_LDFLAGS_ALLOW='-Wl,--(?:no-)?whole-archive' go build


It is the right solution !

I add an ugly CGO_LDFLAGS_ALLOW=’*’ and it works

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.