• Uncategorized

About c : ld—export-dynamic-for-just-one-library

Question Detail

It’s possible to pass --export-dynamic to ld and this will export symbols in the program (so that they are available to any shared libraries loaded at run-time):

$ cat > test.c

void foo() {}

int main() { foo(); }

^D
$ gcc test.c
$ nm -D a.out | grep foo

…nothing. And now:

$ gcc -Wl,--export-dynamic test.c
$ nm -D a.out | grep foo
0000000000001129 T foo

…works.

This is documented in https://sourceware.org/binutils/docs-2.34/ld/Options.html#Options

Is it possible to just export symbols from one particular static library?

Given like:

$ gcc myprogram.cc lib1.a lib2.a lib3.a

Say I just wanted to export symbols in the program from lib2.a, but not lib1.a or lib3.a?

I tried:

$ gcc myprogram.cc lib1.a -Wl,--export-dynamic lib2.a -Wl,--no-export-dynamic lib3.a

but it doesn’t work, it looks like --export-dynamic is global.

(The documentation mentions --dynamic-list=listfile but I didn’t understand the format of the file, or how to extract the symbol list from the static library?)

Question Answer

how to extract the symbol list from the static library?

nm staticlib.a | awk 'some parsing here, mostly {print $3}'

didn’t understand the format of the file

I also don’t, but I’ve found this link: https://sourceware.org/legacy-ml/binutils/2010-01/msg00416.html . The file should contain:

{
   foo;
};

ld –export-dynamic for just one library?

Untested:

gcc myprogram.cc lib1.a lib2.a \
    -Wl,--dynamic-list=<(echo '{'; nm lib1.a | awk '{print $3";"}'; echo '};')

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.