I have been trying to get one of our company’s project that leverages CGO and builds on Linux to build on macOS. To that end, I created a little CGO Hello World test project.
I can get my test project to build and run executables and it respects my environment setting for DYLD_LIBRARY_PATH just fine (since macOS System Integrity Protection will allow DYLD_LIBRARY_PATH to be inherited by non-system executables – i.e.: executables that exist in non-system locations).
HOWEVER, when I run “go test” it fails to find my native library on DYLD_LIBRARY_PATH. I tried setting the environment variable CGO_LDFLAGS and that changes the output that I get, but it still ultimately complains about not finding the native library.
Here is the output without setting CGO_LDFLAGS:
% go test
# localhost/cgo2.test
/Users/barry/dev/go/pkg/tool/darwin_amd64/link: running clang failed: exit status 1
ld: library not found for -lhello
clang: error: linker command failed with exit code 1 (use -v to see invocation)
FAIL localhost/cgo2 [build failed]
Here is the output after setting it:
% echo "CGO_LDFLAGS = $CGO_LDFLAGS"; go test
CGO_LDFLAGS = -L/Users/barry/testdir/
dyld[68952]: Library not loaded: libhello.so
Referenced from: <0F30CBC8-09BB-38FF-A929-66A67266A540> /private/var/folders/fl/yvgrj4p53w7blwlr0zwtf_5h0000gn/T/go-build1534663025/b001/cgo2.test
Reason: tried: 'libhello.so' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibhello.so' (no such file), 'libhello.so' (no such file), '/usr/local/lib/libhello.so' (no such file), '/usr/lib/libhello.so' (no such file, not in dyld cache), '/Users/barry/testdir/chello/libhello.so' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/barry/testdir/chello/libhello.so' (no such file), '/Users/barry/testdir/chello/libhello.so' (no such file), '/usr/local/lib/libhello.so' (no such file), '/usr/lib/libhello.so' (no such file, not in dyld cache)
signal: abort trap
FAIL localhost/cgo2 0.104s
My educated guess is that “go test” is spawning a child process using the default system shell and that shell will not inherit the DYLD_LIBRARY_PATH.
Anybody else building/testing Go language applications using CGO on macOS and figured out how to get it to link to dynamic libraries in non-standard locations?
I would really rather not put all my development builds of my native libraries in /usr/local/lib to get this working.
NOTE: I made sure to install the Go executables in my home directory rather than /usr/local/ to make sure macOS did not see “go” as a system executable.