Link as static library to side project error. #371

Closed
opened 2026-01-29 20:42:51 +00:00 by claunia · 2 comments
Owner

Originally created by @slesarev-hub on GitHub (Feb 5, 2021).

I've build library with CMake as said in readme.
Now I'm trying to link Brotli to another C++ project. The problem here simplified as much as possible:

example.cpp (file stored in Brotli project root)

#include "c/include/brotli/decode.h"
#include "c/include/brotli/encode.h"

int main(){
  size_t v;
  uint8_t* in;
  uint8_t* out;
  BrotliEncoderCompress(BROTLI_DEFAULT_QUALITY, BROTLI_DEFAULT_WINDOW,
                        BrotliEncoderMode::BROTLI_MODE_GENERIC, 1,
                        in, &v, out);
  return 0;
}

build from terminal
g++ -o example example.cpp -Lbuild -lbrotlicommon-static -lbrotlidec-static -lbrotlienc-static -Ic/include

error log

/usr/bin/ld: build/libbrotlienc-static.a(encode.c.o): in function `EncodeData':
encode.c:(.text+0xfb9): undefined reference to `_kBrotliContextLookupTable'
/usr/bin/ld: build/libbrotlienc-static.a(encode.c.o): in function `BrotliCompressBufferQuality10':
encode.c:(.text+0x54e3): undefined reference to `_kBrotliContextLookupTable'
/usr/bin/ld: build/libbrotlienc-static.a(encoder_dict.c.o): in function `BrotliInitEncoderDictionary':
encoder_dict.c:(.text+0x20): undefined reference to `BrotliGetDictionary'
/usr/bin/ld: encoder_dict.c:(.text+0x28): undefined reference to `BrotliGetTransforms'
/usr/bin/ld: build/libbrotlienc-static.a(memory.c.o): in function `BrotliInitMemoryManager':
memory.c:(.text+0x24): undefined reference to `BrotliDefaultAllocFunc'
/usr/bin/ld: memory.c:(.text+0x33): undefined reference to `BrotliDefaultFreeFunc'
/usr/bin/ld: build/libbrotlienc-static.a(brotli_bit_stream.c.o): in function `StoreSymbol':
brotli_bit_stream.c:(.text+0x34a): undefined reference to `_kBrotliPrefixCodeRanges'
/usr/bin/ld: build/libbrotlienc-static.a(brotli_bit_stream.c.o): in function `BuildAndStoreBlockSplitCode':
brotli_bit_stream.c:(.text+0x189b): undefined reference to `_kBrotliPrefixCodeRanges'
/usr/bin/ld: brotli_bit_stream.c:(.text+0x1b40): undefined reference to `_kBrotliPrefixCodeRanges'
/usr/bin/ld: build/libbrotlienc-static.a(brotli_bit_stream.c.o): in function `BrotliStoreMetaBlock':
brotli_bit_stream.c:(.text+0x27c8): undefined reference to `_kBrotliContextLookupTable'
/usr/bin/ld: brotli_bit_stream.c:(.text+0x3104): undefined reference to `_kBrotliPrefixCodeRanges'
/usr/bin/ld: brotli_bit_stream.c:(.text+0x3607): undefined reference to `_kBrotliPrefixCodeRanges'
/usr/bin/ld: build/libbrotlienc-static.a(histogram.c.o): in function `BrotliBuildHistogramsWithContext':
histogram.c:(.text+0x1a8): undefined reference to `_kBrotliContextLookupTable'
collect2: error: ld returned 1 exit status
Originally created by @slesarev-hub on GitHub (Feb 5, 2021). I've build library with CMake as said in readme. Now I'm trying to link Brotli to another C++ project. The problem here simplified as much as possible: example.cpp (file stored in Brotli project root) ``` #include "c/include/brotli/decode.h" #include "c/include/brotli/encode.h" int main(){ size_t v; uint8_t* in; uint8_t* out; BrotliEncoderCompress(BROTLI_DEFAULT_QUALITY, BROTLI_DEFAULT_WINDOW, BrotliEncoderMode::BROTLI_MODE_GENERIC, 1, in, &v, out); return 0; } ``` build from terminal `g++ -o example example.cpp -Lbuild -lbrotlicommon-static -lbrotlidec-static -lbrotlienc-static -Ic/include` error log ``` /usr/bin/ld: build/libbrotlienc-static.a(encode.c.o): in function `EncodeData': encode.c:(.text+0xfb9): undefined reference to `_kBrotliContextLookupTable' /usr/bin/ld: build/libbrotlienc-static.a(encode.c.o): in function `BrotliCompressBufferQuality10': encode.c:(.text+0x54e3): undefined reference to `_kBrotliContextLookupTable' /usr/bin/ld: build/libbrotlienc-static.a(encoder_dict.c.o): in function `BrotliInitEncoderDictionary': encoder_dict.c:(.text+0x20): undefined reference to `BrotliGetDictionary' /usr/bin/ld: encoder_dict.c:(.text+0x28): undefined reference to `BrotliGetTransforms' /usr/bin/ld: build/libbrotlienc-static.a(memory.c.o): in function `BrotliInitMemoryManager': memory.c:(.text+0x24): undefined reference to `BrotliDefaultAllocFunc' /usr/bin/ld: memory.c:(.text+0x33): undefined reference to `BrotliDefaultFreeFunc' /usr/bin/ld: build/libbrotlienc-static.a(brotli_bit_stream.c.o): in function `StoreSymbol': brotli_bit_stream.c:(.text+0x34a): undefined reference to `_kBrotliPrefixCodeRanges' /usr/bin/ld: build/libbrotlienc-static.a(brotli_bit_stream.c.o): in function `BuildAndStoreBlockSplitCode': brotli_bit_stream.c:(.text+0x189b): undefined reference to `_kBrotliPrefixCodeRanges' /usr/bin/ld: brotli_bit_stream.c:(.text+0x1b40): undefined reference to `_kBrotliPrefixCodeRanges' /usr/bin/ld: build/libbrotlienc-static.a(brotli_bit_stream.c.o): in function `BrotliStoreMetaBlock': brotli_bit_stream.c:(.text+0x27c8): undefined reference to `_kBrotliContextLookupTable' /usr/bin/ld: brotli_bit_stream.c:(.text+0x3104): undefined reference to `_kBrotliPrefixCodeRanges' /usr/bin/ld: brotli_bit_stream.c:(.text+0x3607): undefined reference to `_kBrotliPrefixCodeRanges' /usr/bin/ld: build/libbrotlienc-static.a(histogram.c.o): in function `BrotliBuildHistogramsWithContext': histogram.c:(.text+0x1a8): undefined reference to `_kBrotliContextLookupTable' collect2: error: ld returned 1 exit status ```
Author
Owner

@deymo commented on GitHub (Feb 5, 2021):

Hi,
I think this is due to the order of the -l parameters being wrong (but I didn't test this specific example myself). You need to pass -lbrotlienc-static before -lbrotlicommon-static because enc depends on common, and the same for dec. I know it feels backwards but that's how the linker works, here's a nice article about why.

Could you try that?

@deymo commented on GitHub (Feb 5, 2021): Hi, I think this is due to the order of the `-l` parameters being wrong (but I didn't test this specific example myself). You need to pass `-lbrotlienc-static` before `-lbrotlicommon-static` because `enc` depends on `common`, and the same for `dec`. I know it feels backwards but that's how the linker works, here's a [nice article about why](https://eli.thegreenplace.net/2013/07/09/library-order-in-static-linking). Could you try that?
Author
Owner

@slesarev-hub commented on GitHub (Feb 5, 2021):

It helps, thank you!

@slesarev-hub commented on GitHub (Feb 5, 2021): It helps, thank you!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/brotli#371