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

后端 未结 2 1055
天涯浪人
天涯浪人 2020-12-15 01:05

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

Specifically, since it\'s

2条回答
  •  悲&欢浪女
    2020-12-15 01:50

    Since you declare

    int global;
    

    in the multiply.h header, the DLL and main program both have their own copy of it. Instead, declare the global in main.c

    int global;
    

    and in multiply.c declare it as extern:

    extern int global;
    

    Now if you link main.cpp with the -rdynamic option, then the executable's symbols will get exported to the DLL.

    I tested this under linux and it worked, but I'm afraid I dont have access to test on MacOS. Since your ssample code also didn't work on linux, I expect this was the problem.

提交回复
热议问题