Is it possible to build shared libprotobuf.so with static abls libraries

1,983 views
Skip to first unread message

Adam Dembek

unread,
Jun 16, 2023, 5:59:43 AM6/16/23
to Protocol Buffers
We want to use shared libprotobuf.so for many tools to not increase size of each binary.
We know we can build protobuf with -Dprotobuf_BUILD_SHARED_LIBS=ON -Dprotobuf_INSTALL_SUPPORTED_FROM_MODULE=ON -Dprotobuf_ABSL_PROVIDER=module but then both protobuf and absl are build as shared library.
We need to deliver over 80 libraries.  It would easier if we could only deliver single libprotobuf.so file like in the past when protobuf was not using absl.

Is it possible to use static absl libraies and link it libprotobuf.so ?
I tried firts build abls independentlay with

cmake -S ${ABSL_SOURCE} -B ${ABSL_PROD_DIR} -DCMAKE_PREFIX_PATH=${ABSL_PROD_DIR} -DCMAKE_INSTALL_PREFIX=${ABSL_PROD_DIR} -DABSL_ENABLE_INSTALL=ON -DABSL_USE_EXTERNAL_GOOGLETEST=OFF -DABSL_FIND_GOOGLETEST=OFF

This created static absl libraries.

And then build protobuf  with  -DCMAKE_PREFIX_PATH=${ABSL_PROD_DIR}/lib64/cmake/absl -Dprotobuf_ABSL_PROVIDER=package-DCMAKE_CXX_STANDARD=14 -Dprotobuf_BUILD_SHARED_LIBS=ON

But it fails  with

[ 36%] Linking CXX shared library libprotobuf.so
/usr/bin/ld: /user/adembek/PROD/absl/build/absl_2023_01_25/lib64/libabsl_cord.a(cord.cc.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /user/adembek/PROD/absl/build/absl_2023_01_25/lib64/libabsl_cord.a(cord_analysis.cc.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /user/adembek/PROD/absl/build/absl_2023_01_25/lib64/libabsl_die_if_null.a(die_if_null.cc.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC

Mike Kruskal

unread,
Jun 16, 2023, 1:04:02 PM6/16/23
to Protocol Buffers
Hey Adam,

You can try to set `POSITION_INDEPENDENT_CODE` in the Abseil build.  That should at least get around the error you're seeing here.  However, the reason we don't explicitly support this is because in many situations it's an ODR violation.  If two shared libraries both use Abseil via static linkage, there will be two definitions of absl.  Assuming they're both identical, this isn't a problem in most of the absl code.  However, when it comes to things like the hash seed you can end up breaking every hash table in very difficult to debug ways.  I think the only way this could work is if none of the other shared libraries (and the code that's using them) were using Abseil, and you don't need protoc (which also uses Abseil).

That seems like a pretty brittle requirement to place on anyone, and I think a feature request for Abseil to ship a single shared library might be a better approach.  They already do this for windows, but not on linux or mac.

-Mike

Adam Dembek

unread,
Jun 17, 2023, 1:25:39 AM6/17/23
to Protocol Buffers
Thanks

We need absl only for  protobuf  and grpc. Right now  we plan to build abls static  libraries and link the same files to both protobuf  and  grpc. When this  will not work  we  can try building shared libarary.

Adam
Reply all
Reply to author
Forward
0 new messages