{"id":682,"date":"2022-08-30T15:05:25","date_gmt":"2022-08-30T15:05:25","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/accessing-main-program-global-variables-from-a-dlopened-dynamic-library-in-c-on-os-x-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:05:25","modified_gmt":"2022-08-30T15:05:25","slug":"accessing-main-program-global-variables-from-a-dlopened-dynamic-library-in-c-on-os-x-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/accessing-main-program-global-variables-from-a-dlopened-dynamic-library-in-c-on-os-x-collection-of-common-programming-errors\/","title":{"rendered":"Accessing main program global variables from a dlopen()ed dynamic library in C on OS X-Collection of common programming errors"},"content":{"rendered":"<p>I am maintaining a small application that has some plugin-like functionality, which is implemented through runtime-loaded dynamic modules.<\/p>\n<p>Specifically, since it&#8217;s a Gtk+ app, I&#8217;m using gmodule, but the question applies to dlfcn.h \/ dlopen() based dynamic library loading just as well.<\/p>\n<p>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.<\/p>\n<p>On Linux, I could just refer to this global variable directly &#8211; this works well, and I&#8217;m guessing that gcc or the linker take care of exporting the global variables from the main program to dynamic libraries.<\/p>\n<p>Problem is, this doesn&#8217;t work on Mac OS X.<\/p>\n<p>Is there a way to do this on OS X?<\/p>\n<p>If not, is there a more &#8220;best practice&#8221; way to expose global information to dynamically loaded libraries?<\/p>\n<p>Thanks!<\/p>\n<ol>\n<li>\n<p>Put the global in main.c and declare it extern in the shared object, and try this:<\/p>\n<pre><code>MACOSX_DEPLOYMENT_TARGET=10.3 ld -dylib -undefined dynamic_lookup -o multiply.so multiply.o\n<\/code><\/pre>\n<p>or<\/p>\n<pre><code>MACOSX_DEPLOYMENT_TARGET=10.3 libtool -dynamic -undefined dynamic_lookup -o multiply.so multiply.o\n<\/code><\/pre>\n<p>It worked for me on Mac OS X 10.4<\/p>\n<\/li>\n<li>\n<p>Since you declare<\/p>\n<pre><code>int global;\n<\/code><\/pre>\n<p>in the multiply.h header, the DLL and main program both have their own copy of it. Instead, declare the global in main.c<\/p>\n<pre><code>int global;\n<\/code><\/pre>\n<p>and in multiply.c declare it as extern:<\/p>\n<pre><code>extern int global;\n<\/code><\/pre>\n<p>Now if you link main.cpp with the -rdynamic option, then the executable&#8217;s symbols will get exported to the DLL.<\/p>\n<p>I tested this under linux and it worked, but I&#8217;m afraid I dont have access to test on MacOS. Since your ssample code also didn&#8217;t work on linux, I expect this was the problem.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:38:34. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am maintaining a small application that has some plugin-like functionality, which is implemented through runtime-loaded dynamic modules. Specifically, since it&#8217;s a Gtk+ app, I&#8217;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. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-682","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/682","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=682"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/682\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=682"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=682"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=682"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}