3

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.

rjni
  • 471
  • 7
  • 7
  • You can't build with `CGO_ENABLED=0` if your application calls out to a C library, whether the C library is static or shared. https://blog.madewithdrew.com/post/statically-linking-c-to-go/ seems describe how it can be solved, though I haven't actually tried it. Your other options are to ensure that users running your application must have the proper libraries installed, or even better, build a Docker image with app and its dependencies installed and share that. – svsd Sep 20 '18 at 03:19
  • Yes, I had already tried without CGO_ENABLED=0 but it didn't actually have any effect. I totally agree with you on docker image - but if the usecase had permitted. This is a small application which is supposed to be plug and play rather than having to do a setup. – rjni Sep 25 '18 at 23:52

0 Answers0