linux - Go CGO: Binary doesn't find shared library with rpath, only works with LD_LIBRARY_PATH - Stack Overflow

admin2025-04-17  3

I am working on a GO project that links to a shared library (.so) using CGO. I want to embed the library path in the binary using rpath,so that it runs without needing to set the LD_LIBRARY_PATH. However, my Go test binary only works when I manually set the LD_LIBRARY_PATH but fails when relying on rpath alone.

This works:

unit-test:
@(export LD_LIBRARY_PATH=${LIB_PATH}:${LD_LIBRARY_PATH}; \
        CGO_LDFLAGS="-Wl,-rpath,${LIB_PATH} -L${LIB_PATH} -l<some_lib>" ${GOEXE} test -coverprofile=coverage.out ./...)

but the following doesn't

@CGO_LDFLAGS="-Wl,-rpath,${LIB_PATH} -L${LIB_PATH} -l<some_lib>" 
        ${GOEXE} test -coverprofile=coverage.out ./...

I have tried this on both Ubuntu 22.04 and OpenSuse 15. Am I missing any configuration?

Thanks!

转载请注明原文地址:http://anycun.com/QandA/1744850111a88492.html