linxGnu / grocksdb

RocksDB wrapper for Go. Support 9.x, 8.x, 7.x, 6.x, etc
MIT License
315 stars 68 forks source link

librocksdb.so.8 not found when using CGO_LDFLAGS #149

Closed chloeyin closed 6 months ago

chloeyin commented 7 months ago

I tried to build the go program with RocksDB installed not in the default libraray search path and met the below problem. I'm using Linux Homebrew to manage my RocksDB library.

Using the doc's command

CGO_CFLAGS="-I/home/linuxbrew/.linuxbrew/include" \
CGO_LDFLAGS="-L/home/linuxbrew/.linuxbrew/lib -lrocksdb -lstdc++ -lm -lz -lsnappy -llz4 -lzstd" \
go build cmd/main.go
./main
./main: error while loading shared libraries: librocksdb.so.8: cannot open shared object file: No such file or directory

The output from ldd is:

ldd main
        linux-vdso.so.1 (0x00007ffcd85f5000)
        librocksdb.so.8 => not found
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f75f93ad000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f75f95df000)

This is the library from the -L path

ls -lgo /home/linuxbrew/.linuxbrew/lib/librocksdb*
lrwxrwxrwx 1 40 Dec 13 17:06 /home/linuxbrew/.linuxbrew/lib/librocksdb.a -> ../Cellar/rocksdb/8.9.1/lib/librocksdb.a
lrwxrwxrwx 1 41 Dec 13 17:06 /home/linuxbrew/.linuxbrew/lib/librocksdb.so -> ../Cellar/rocksdb/8.9.1/lib/librocksdb.so
lrwxrwxrwx 1 43 Dec 13 17:06 /home/linuxbrew/.linuxbrew/lib/librocksdb.so.8 -> ../Cellar/rocksdb/8.9.1/lib/librocksdb.so.8
lrwxrwxrwx 1 47 Dec 13 17:06 /home/linuxbrew/.linuxbrew/lib/librocksdb.so.8.9.1 -> ../Cellar/rocksdb/8.9.1/lib/librocksdb.so.8.9.1
objdump -p /home/linuxbrew/.linuxbrew/lib/librocksdb.so | grep -i SONAME
  SONAME               librocksdb.so.8

Since during the build time, it is able to resolve the library librocksdb.so.8 from the SONAME, the problem should be related to the runtime library resolve.

So, for those who doesn't put library in the default search path, should we suggest them add -rpath when compiling the go program? After specifying -rpath

CGO_CFLAGS="-I/home/linuxbrew/.linuxbrew/include" \
CGO_LDFLAGS="-L/home/linuxbrew/.linuxbrew/lib -lrocksdb -lstdc++ -lm -lz -lsnappy -llz4 -lzstd \
-Wl,-rpath=/home/linuxbrew/.linuxbrew/lib" \
go build cmd/main.go

The ldd output is ok.

ldd main
        linux-vdso.so.1 (0x00007ffc652fd000)
        librocksdb.so.8 => /home/linuxbrew/.linuxbrew/lib/librocksdb.so.8 (0x00007fb7722c2000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fb772092000)
        libgflags.so.2.2 => /home/linuxbrew/.linuxbrew/opt/gflags/lib/libgflags.so.2.2 (0x00007fb772066000)
        libsnappy.so.1 => /home/linuxbrew/.linuxbrew/opt/snappy/lib/libsnappy.so.1 (0x00007fb772058000)
        libz.so.1 => /home/linuxbrew/.linuxbrew/opt/zlib/lib/libz.so.1 (0x00007fb77203d000)
        libbz2.so.1.0 => /home/linuxbrew/.linuxbrew/opt/bzip2/lib/libbz2.so.1.0 (0x00007fb772029000)
        liblz4.so.1 => /home/linuxbrew/.linuxbrew/opt/lz4/lib/liblz4.so.1 (0x00007fb771e00000)
        libzstd.so.1 => /home/linuxbrew/.linuxbrew/opt/zstd/lib/libzstd.so.1 (0x00007fb771d17000)
        libstdc++.so.6 => /home/linuxbrew/.linuxbrew/opt/gcc/lib/gcc/current/libstdc++.so.6 (0x00007fb771a61000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fb77197a000)
        libgcc_s.so.1 => /home/linuxbrew/.linuxbrew/opt/gcc/lib/gcc/current/libgcc_s.so.1 (0x00007fb771953000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fb772f0c000)

I'm not sure if there is some problem with my machine, at least this is the only solution for me. Also, I search for the issue but don't find any solutions, so just raise it here :). But I think this works only if we build and run on the same machine, doc this point may help.

linxGnu commented 6 months ago

I don't think adding -rpath might help the users of grocksdb. I did not use linuxbrew yet, can not give recommendation to fix your issue without adding -rpath.

@yihuang might help 🤔

yihuang commented 6 months ago

Just set the export LD_LIBRARY_PATH=/home/linuxbrew/.linuxbrew/lib when running it?

chloeyin commented 6 months ago

Thanks, it's another option.