<story><title>Anatomy of Linux Dynamic Libraries</title><url>https://developer.ibm.com/tutorials/l-dynamic-libraries/</url></story><parent_chain></parent_chain><comment><author>chrsig</author><text>This is a good primer on the subject.&lt;p&gt;One of the major projects I maintain at $work is an apache module, which has given me so much grief because of the fact that it gets dynamically loaded (and then unloaded, and then reloaded)&lt;p&gt;It&amp;#x27;s been very enlightening, but there&amp;#x27;s no shortage of frustration.&lt;p&gt;One major points is that a dynamically loaded library can have undefined symbols that are found (or not!) at runtime.  This can introduce a lot of trouble when trying to make a test binary.  Especially in apache&amp;#x27;s case, since any of the `ap_` symbols are only compiled into the executable, and aren&amp;#x27;t available as a static or shared library.&lt;p&gt;In that situation, the only recourse that I&amp;#x27;m aware of is to supply your own implementations of those functions that get compiled into the test binary (&lt;i&gt;not&lt;/i&gt; the dynamic library under test, since that will create a collision when it&amp;#x27;s loaded)&lt;p&gt;Another thing to consider is that linux&amp;#x2F;elf shared libraries can be either dynamically loaded or dynamically linked.  This is not true on all platforms, so it&amp;#x27;s not advisable to lean into it if portability matters.&lt;p&gt;I recommend reading the ld.so manpage[0] -- in particular it outlines many environment variables to control the runtime linker.  Notably, LD_DEBUG can be a life saver.&lt;p&gt;Lastly, I&amp;#x27;ll also warn against using any global&amp;#x2F;static memory in a dynamically loaded library.  It can be unloaded&amp;#x2F;reloaded, and that can create a lot of havoc, especially if a pointer to that memory gets saved somewhere that survives the unload, and then is accessible after the library gets reloaded.&lt;p&gt;libprotobuf is a major victim of these types of issues - which is why (or a major contributor) to why the libprotobuf-lite library exists.&lt;p&gt;[0] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;man7.org&amp;#x2F;linux&amp;#x2F;man-pages&amp;#x2F;man8&amp;#x2F;ld.so.8.html&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;man7.org&amp;#x2F;linux&amp;#x2F;man-pages&amp;#x2F;man8&amp;#x2F;ld.so.8.html&lt;/a&gt;</text></comment>
<story><title>Anatomy of Linux Dynamic Libraries</title><url>https://developer.ibm.com/tutorials/l-dynamic-libraries/</url></story><parent_chain></parent_chain><comment><author>coley</author><text>The first time I learned about dynamic loading was while watching an episode of Casey Muratori&amp;#x27;s Handmade Hero - a series where he builds a video game, in C, from scratch.&lt;p&gt;He compiles the game code as a dll and dynamically loads it at runtime in the Win32 platform layer code. This way he can keep platform code and game code separate and reload the game code at runtime if any changes are made. Being new to this technique, I was impressed to say the least.&lt;p&gt;I always assumed the same functionality was available in the linux environment, but I hadn&amp;#x27;t bothered to look it up. Now I know.&lt;p&gt;For those who want to learn how to do this in Win32, here&amp;#x27;s the episode I mentioned above - &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;www.youtube.com&amp;#x2F;watch?v=WMSBRk5WG58&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;www.youtube.com&amp;#x2F;watch?v=WMSBRk5WG58&lt;/a&gt;</text></comment>