1

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?)

1

1 Answer 1

2

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 '};')
Sign up to request clarification or add additional context in comments.

1 Comment

Did this work for anyone? I tried exporting a symbol from a shared library like that (using LD) and it didn't work for me. Do the symbols in the archives need to be declared as dynamic already?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.