Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

shared libraries - Receive "undefined symbol" error when loading library with dlopen

I'm writing some code that uses dynamic shared libraries as plugins.

My command line for building the shared libraries looks like:

cc -shared -fPIC -o module.so -g -Wall module.c

Within the module, I can call functions that are in any other shared library that has been loaded within the main executable.

However I cannot access (exported) functions that are in the executable itself (I get undefined symbol errors).

My call to dlopen looks like this:

void *handle = dlopen(plugin, RTLD_NOW);

Can anyone please advise how my module can call back to my executable, without having to put all of the executable's utility functions into yet another shared library?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Correct solution is to add -rdynamic to the link command of the main executable. This will add appropriate option to ld (which, when using GNU ld, happens to be --export-dynamic).

Adding --export-dynamic directly is technically incorrect: it's a linker option, and so should be added as -Wl,--export-dynamic, or -Wl,-E. This is also less portable than -rdynamic (other linkers have an equivalent, but the option itself is different).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...