How to always include characters from a static library?

Suppose I have a libx.a static library. How to make some characters (not all) from this library always present in any binary link I have with my library? The reason is that I need these characters to access through dlopen + dlsym. I know the wholes-archive linker switch, but it forces all the object files from the library archive to merge into the resulting binary, and this is not what I want ...

Observations so far (CentOS 5.4, 32bit) ( upd : this paragraph is incorrect, I could not reproduce this behavior)

ld main.o libx.a

happy to share all the characters without links, and

ld main.o -L. -lx

will link the entire library. I assume that this depends on the version of binutils used, and new linkers will be able to select individual objects from the static library.

Another question: how can I achieve the same effect on Windows?

Thanks in advance. Any hints would be greatly appreciated.

+3
source share
4 answers

Imagine that you have a project that consists of the following three C files in the same folder:

// ---- jam.h
 int jam_badger(int);

// ---- jam.c
 #include "jam.h"
 int jam_badger(int a)
 {
   return a + 1;
 }

 // ---- main.c
 #include "jam.h"
 int main()
 {
   return jam_badger(2);
 }

And you will create it using a bjam file for formatting, for example:

lib jam : jam.c <link>static ;

lib jam_badger : jam ;

exe demo : jam_badger main.c ;

You will receive an error message.

undefined reference to `jam_badger'

(I used bjam here because the file is easier to read, but you can use whatever you want)

"" , ( ).

, ld , , .

, -Wl, - -Wl, - --, :

g++ -o "libjam_candle_badger.so" -Wl,--whole-archive libjam_badger.a Wl,--no-whole-archive

, boost-build, , .

+3

: ld main.o libx.a . , ld ; (gcc ).

, "ld main.o libx.a" "ld main.o -L. -lx" . , .

, : , foo, bar baz a.out, :

gcc -Wl,-u,foo,-u,bar,-u,baz main.o -L. -lx -rdynamic

:
: ", , ", : , ? - ( dlsym), - API .

, . .

+2

, , , libx.a.

+1

, .

If the gcc optimizer fixes this anyway, do something with this address - that should be enough.

+1
source

Source: https://habr.com/ru/post/1723340/


All Articles