title: How to force inclusion of an object file in a static library when linking into executable?
body: <p>I have a C++ project that due to its directory structure is set up as a static library <code>A</code>, which is linked into shared library <code>B</code>, which is linked into executable <code>C</code>.  (This is a cross-platform project using CMake, so on Windows we get <code>A.lib</code>, <code>B.dll</code>, and <code>C.exe</code>, and on Linux we get <code>libA.a</code>, <code>libB.so</code>, and <code>C</code>.)  Library <code>A</code> has an init function (<code>A_init</code>, defined in <code>A/initA.cpp</code>), that is called from library <code>B</code>'s init function (<code>B_init</code>, defined in <code>B/initB.cpp</code>), which is called from <code>C</code>'s main.  Thus, when linking <code>B</code>, <code>A_init</code> (and all symbols defined in <code>initA.cpp</code>) is linked into <code>B</code> (which is our desired behavior).</p>
  
  <p>The problem comes in that the <code>A</code> library also defines a function (<code>Af</code>, defined in <code>A/Afort.f</code>) that is intended to by dynamically loaded (i.e. <code>LoadLibrary</code>/<code>GetProcAddress</code> on Windows and <code>dlopen</code>/<code>dlsym</code> on Linux).  Since there are no references to <code>Af</code> from library <code>B</code>, symbols from <code>A/Afort.o</code> are not included into <code>B</code>.  On Windows, we can artifically create a reference by using the pragma:</p>
  
  <pre><code>#pragma comment (linker, "/export:_Af")
  </code></pre>
  
  <p>Since this is a pragma, it only works on Windows (using Visual Studio 2008).  To get it working on Linux, we've tried adding the following to <code>A/initA.cpp</code>:</p>
  
  <pre><code>extern void Af(void);
  static void (*Af_fp)(void) = &amp;Af;
  </code></pre>
  
  <p>This does not cause the symbol <code>Af</code> to be included in the final link of <code>B</code>.  How can we force the symbol <code>Af</code> to be linked into <code>B</code>?</p>