0

Hi wonderful people of Stackoverflow:

I'm using cross compilers for my go project which has used rocksdb, an embedded key-value storage written in c++. I execute

CGO_LDFLAGS="-static -L/opt/homebrew/Cellar/rocksdb@7.7.3/7.7.3/lib -L/opt/homebrew/Cellar/zstd/1.5.5/lib -L/opt/homebrew/Cellar/lz4/1.9.4/lib -L/opt/homebrew/Cellar/snappy/1.1.10/lib -L/opt/homebrew/Cellar/zlib/1.2.13/lib" CGO_CFLAGS="-I/opt/homebrew/cellar/rocksdb@7.7.3/7.7.3/include" CGO_ENABLED=1 GOOS=linux  GOARCH=amd64 CC=x86_64-unknown-linux-gnu-gcc CXX=x86_64-unknown-linux-gnu-g++ go build -o yeeeeeeeeet

and the result is as follows:

...
/opt/homebrew/Cellar/x86_64-unknown-linux-gnu/11.2.0_1/toolchain/bin/../lib/gcc/x86_64-unknown-linux-gnu/11.2.0/../../../../x86_64-unknown-linux-gnu/bin/ld.bfd: /var/folders/85/7xzvr1n56wnbx8tj6h38drq80000gn/T/go-link-2911826502/000054.o: in function `gorocksdb_compactionfilter_create':
grocksdb.c:(.text+0xbe): undefined reference to `rocksdb_compactionfilter_create'
/opt/homebrew/Cellar/x86_64-unknown-linux-gnu/11.2.0_1/toolchain/bin/../lib/gcc/x86_64-unknown-linux-gnu/11.2.0/../../../../x86_64-unknown-linux-gnu/bin/ld.bfd: /var/folders/85/7xzvr1n56wnbx8tj6h38drq80000gn/T/go-link-2911826502/000054.o: in function `gorocksdb_mergeoperator_create':
grocksdb.c:(.text+0x104): undefined reference to `rocksdb_mergeoperator_create'
/opt/homebrew/Cellar/x86_64-unknown-linux-gnu/11.2.0_1/toolchain/bin/../lib/gcc/x86_64-unknown-linux-gnu/11.2.0/../../../../x86_64-unknown-linux-gnu/bin/ld.bfd: /var/folders/85/7xzvr1n56wnbx8tj6h38drq80000gn/T/go-link-2911826502/000054.o: in function `gorocksdb_slicetransform_create':
grocksdb.c:(.text+0x16d): undefined reference to `rocksdb_slicetransform_create'
collect2: error: ld returned 1 exit status

When I'm not building for the Linux environment and straight on using the default cgo compiler, non of these error messages would have happened. I think the error messages mean that the cross-compiler can't find any of the header files of rocksdb, and there's a high probability that the CGO_CFLAGS environment variable has not been correctly passed on to the c and c++ cross-compiler during build.

I'm humbly asking for any kind of assistance or hints that can potentially help me get this over with. Any assistance is utmostly appreciated.

I wish more people with cgo cross-compiling experience, especially the good folks who have used https://github.com/FiloSottile/homebrew-musl-cross or https://github.com/messense/homebrew-macos-cross-toolchains can come forward.

  • Those are link errors, and you're not actually linking against anything (`-L` modifies the directories the linker will search for libraries). You'll have to figure out the actual libraries you need, linked dupe has the appropriate flags you can afterwards. – Stephen Newell Jun 29 '23 at 04:59
  • @StephenNewell isn't `CGO_CFLAGS="-I/opt/homebrew/cellar/rocksdb@7.7.3/7.7.3/include"` already does that? – user19884734 Jun 29 '23 at 05:54
  • I don't know why my question has been marked as duplicate and linked to a very generic c compiler question that has been answered around 10 years ago, in my opinion this question isn't just about c, it's about cross-compiling for Go development. Please reconsider the decision of closing this question. – user19884734 Jun 29 '23 at 06:15
  • `-I` tells the compiler where to look for header files. What you need it to actually add a library to link. With `-L` You only tell where the library might be but not what library to take. Use `-l` for that. – Gerhardh Jun 29 '23 at 08:10
  • thanks for the assistance @Gerhardh, I've added -lrocksdb into the CGO_LDFLAGS, the revised `go build` command is `CGO_LDFLAGS="-lrocksdb -L/opt/homebrew/Cellar/rocksdb@7.7.3/7.7.3/lib -L/opt/homebrew/Cellar/zstd/1.5.5/lib -L/opt/homebrew/Cellar/lz4/1.9.4/lib -L/opt/homebrew/Cellar/snappy/1.1.10/lib -L/opt/homebrew/Cellar/zlib/1.2.13/lib" CGO_CFLAGS="-I/opt/homebrew/cellar/rocksdb@7.7.3/7.7.3/include" CGO_ENABLED=1 GOOS=linux GOARCH=amd64 CC=x86_64-linux-gnu-gcc CXX=x86_64-linux-gnu-g++ go build -x -o yeeeeeeeeet`, but the compiler still can't find the header files for rocksdb, same error. – user19884734 Jun 29 '23 at 08:45
  • Can you see somewhere how the compiler and linker commands look when they are called? For the linker it is important to put the libaries after the object files and the output file. Unfortunately I have no clue of that Go part... – Gerhardh Jun 29 '23 at 10:28
  • BTW: The compiler does not complain about any headers. As the linked duplicate explains, the linker complains about a missing symbol. The headers were found. Otherwise your compiler would complain about using functions without declaration. In your case, the linker does not know where the symbols are implemented. Therefore it is missing a library. In C, you need to distinguish compiler errors and linker errors. – Gerhardh Jun 29 '23 at 10:45
  • @Gerhardh and that's why this question should not be tagged as duplicated, albeit related to c, it's not a vanilla c compiler issue... – user19884734 Jun 29 '23 at 11:53
  • I didn't vote to close as duplicate. – Gerhardh Jun 29 '23 at 11:58
  • Yes I can see that. Thanks very much for the assist. Some of the output during go build is as follows: – user19884734 Jun 29 '23 at 12:01
  • Unfortunately I can't type in the output as it exceeds the discussion words limit. – user19884734 Jun 29 '23 at 12:01

0 Answers0