4
votes

I am trying to build my .so library with all dependencies (mostly boost) statically linked. Currently, I can build statically linked static library and dynamically linked shared library:

image description

I would like to add other dependencies to .so library so that it has 20MB and does not require user to install anything. Note that this is just a temporary solution before we upgrade to new Boost 1.55 on production servers.

I define libraries like this in Makefile ($ARCH can be either 32 or 64):

## Multi-arch library locations
LIB32=/usr/lib/i386-linux-gnu/
LIB64=/usr/lib/x86_64-linux-gnu/
LIBDIR:=$(LIB$(ARCH))
##Library directory
LIB=-L $(LIBDIR)
## DYNAMIC
LIBS=-lboost_serialization -lboost_thread -lboost_date_time -lboost_signals -lboost_iostreams -lboost_system -llog4cplus -lcrypto -lssl -lm
## STATIC
SLIBS=$(LIBDIR)libboost_serialization.a $(LIBDIR)libboost_thread.a $(LIBDIR)libboost_date_time.a $(LIBDIR)libboost_signals.a $(LIBDIR)libboost_iostreams.a $(LIBDIR)libboost_system.a $(LIBDIR)liblog4cplus.a

Shared lib with dynamic linking:

This is my shared lib GCC command:

  • Makefile:

    $(CXX) $(CFLAGS) $(INCLUDE) $(LIB) $(LIBS) -shared -Wl,-soname,$(SHARED_LIB_VERSION) -o $(NEW_LIB_DIR)${SHARED_LIB_VERSION} $(OBJ_CPP_DYN) $(OBJ_C_DYN)
    
  • Changes into:

    g++ -m64 -Wl,--trace -D NDEBUG -I /usr/include/ -I /usr/local/include -L /usr/lib/x86_64-linux-gnu/ -shared -lboost_serialization -lboost_thread -lboost_date_time -lboost_signals -lboost_iostreams -lboost_system -llog4cplus -lcrypto -lssl -lm -Wl,-soname,libLIBNAMEx64.so -o ../Release/libLIBNAMELIBNAMEx64.so ... and much more .o files ...
    
  • Linker says:

    -lboost_serialization (/usr/lib/x86_64-linux-gnu//libboost_serialization.so)
    -lboost_thread (/usr/lib/x86_64-linux-gnu//libboost_thread.so)
    -lboost_date_time (/usr/lib/x86_64-linux-gnu//libboost_date_time.so)
    -lboost_signals (/usr/lib/x86_64-linux-gnu//libboost_signals.so)
    -lboost_iostreams (/usr/lib/x86_64-linux-gnu//libboost_iostreams.so)
    -lboost_system (/usr/lib/x86_64-linux-gnu//libboost_system.so)
    -llog4cplus (/usr/lib/x86_64-linux-gnu//liblog4cplus.so)
    -lcrypto (/usr/lib/x86_64-linux-gnu//libcrypto.so)
    -lssl (/usr/lib/x86_64-linux-gnu//libssl.so)
    

And it works.

Shared lib with static linking:

I thought I can just replace -shared with -static:

g++ -m64 -Wl,--trace -D NDEBUG -I /usr/include/ -I /usr/local/include  -L /usr/lib/x86_64-linux-gnu/ -static -lboost_serialization -lboost_thread -lboost_date_time -lboost_signals -lboost_iostreams -lboost_system -llog4cplus -lcrypto -lssl -lm -Wl,-soname,libLIBNAMEx64_static_link.so -o ../Release/libLIBNAMEx64_static_link.so ... and much more .o files ...

But I get undefined reference errors everywhere.

So where do I configure how are dependencies linked with my binary? How can I achieve my goal?

More things I tried (edit)

-Wl,--library:

I also tried passing libraries directly to linker in a very assertive manner:

-Wl,--library=:/usr/lib/x86_64-linux-gnu/libboost_serialization.a

And without : (which prevents searching for .a):

 -Wl,--library=/usr/lib/x86_64-linux-gnu/libboost_serialization.a

But I get the error:

  /usr/bin/ld: cannot find -l/usr/lib/x86_64-linux-gnu/libboost_serialization.a

Of course, the file exists at that path.

Pass just library names:

Of course, here it's not even interpreted as linking command:

g++: error: libboost_serialization.a: No such file or directory

Interesting is that if I pass full path instead (/usr/lib/x86_64-linux-gnu/libboost_iostreams.a), GCC doubles it:

g++: error: /usr/lib/x86_64-linux-gnu//usr/lib/x86_64-linux-gnu/libboost_signals.a: No such file or directory
2
Does this question not cover your problem?Michael Foukarakis
g++: error: libboost_date_time.a: No such file or directory - nope, it doesn't.Tomáš Zato - Reinstate Monica
@MichaelFoukarakis I have both static .a and dynamic .so boost libraries. Same applies to the others I'm using. As I said, statically linked static lib compiles without problems.Tomáš Zato - Reinstate Monica

2 Answers

2
votes

Use command man ld, I got this information:

-static

Do not link against shared libraries. It affects library searching for -l options which follow it. This option also implies --unresolved-symbols=report-all. This option can be used with -shared. Doing so means that a shared library is being created but that all of the library's external references must be resolved by pulling in entries from static libraries.

This option is exactly what you want: create a shared library with all dependencies (mostly boost) statically linked.

-l parameter is used to specify the library name, so you should use boost_serialization instead of /path/libboost_serialization.a:

-larchive

If you specify -lcommon, then ld will search its path-list for occurrences of "libcommon.a" for every common specified.

You can use -L parameter many times to specify the library paths when ld try to search static libraries:

-Lsearchdir

For example: -L/usr/lib/x86_64-linux-gnu/

0
votes

You could try -Wl,--whole-archive ${your library} -Wl,--no-whole-archive.