从访问的dlopen主程序全局变量(c)中编动态库在OS X

问题描述:

我保持,有一些插件一样的功能的小应用,该通过运行时动态加载的模块实现。

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

具体,因为它是一个基于GTK +应用程序,我使用gmodule,但问题适用于dlfcn.h时会有麻烦/ dlopen()的基于动态库加载一样好。

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.

在Linux上,我可以直接引用这个全局变量 - 这工作得很好,而且我猜测,GCC或连接采取从主程序动态库导出的全局变量的照顾。

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.

问题是,这并不在Mac OS X的工作是有办法做到这一点在OS X?

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?

把全球main.c中,并在共享对象的extern声明它,试试这个:

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

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

这为我工作在Mac OS X 10.4

It worked for me on Mac OS X 10.4