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
650 views
in Technique[技术] by (71.8m points)

macos - Accessing main program global variables from a dlopen()ed dynamic library in C on OS X

I am maintaining a small application that has some plugin-like functionality, which is implemented through runtime-loaded dynamic modules.

Specifically, since it's a Gtk+ app, I'm using gmodule, but the question applies to dlfcn.h / dlopen() based dynamic library loading just as well.

My main program has a single, global struct variable holding some global information. I want this information to be available to functions defined in the dynamically loaded plugins.

On Linux, I could just refer to this global variable directly - this works well, and I'm guessing that gcc or the linker take care of exporting the global variables from the main program to dynamic libraries.

Problem is, this doesn't work on Mac OS X. Is there a way to do this on OS X?

If not, is there a more "best practice" way to expose global information to dynamically loaded libraries?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Put the global in main.c and declare it extern in the shared object, and try this:

MACOSX_DEPLOYMENT_TARGET=10.3 ld -dylib -undefined dynamic_lookup -o multiply.so multiply.o

or

MACOSX_DEPLOYMENT_TARGET=10.3 libtool -dynamic -undefined dynamic_lookup -o multiply.so multiply.o

It worked for me on Mac OS X 10.4


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

...