How to statically link external Go libraries

2.6k views Asked by At

I have a small application written in Golang which uses Oci8 for Oracle database connectivity. When I attempt to run the binary built on my Macbook on another Macbook (same OS version), it fails with the following error:

dyld: Library not loaded: @rpath/libclntsh.dylib.12.1
  Referenced from: /Users/{username_masked}/Documents/gitRepo/UserRevoke/./user_revoke
  Reason: image not found
Abort trap: 6

I tried to compile with static library as:

go build -a -ldflags '-extldflags "-static"' .

It fails with following error:

/usr/local/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1 /usr/bin/ld: cannot find -lclntsh collect2: ld returned 1 exit status

I then tried to cross-compile as explained at How do you statically link a c library in go using cgo?

That is,

CGO_ENABLED=0 go build -a -installsuffix cgo -ldflags '-s' user_revoke.go

But, it fails with the following error:

# github.com/mattn/go-oci8
/usr/local/go/src/github.com/mattn/go-oci8/oci8_go18.go:13:10: undefined: OCI8Conn

In short, I tried multiple arguments. But, all of them either gives dynamically linked binary or fails to compile at all.

I am not even trying to distribute it between multiple Operating systems. What I am trying to do is build this package for an OS, say Mac, so that it runs on any Mac machines without having to explicitly install the library.

0

There are 0 answers