title: shared library constructor not working
body: <p>In my shared library I have to do certain initialization at the load time. If I define the function with the GCC attribute <code>__attribute__ ((constructor))</code> it doesn't work, i.e. it doesn't get called when the program linking my shared library is loaded.</p>
  
  <p>If I change the function name to <code>_init()</code>, it works. Apparently the usage of <code>_init()</code> and <code>_fini()</code> functions are <a href="http://www.faqs.org/docs/Linux-HOWTO/Program-Library-HOWTO.html#INIT-AND-CLEANUP" rel="noreferrer">not recommended</a> now.</p>
  
  <p>Any idea why <code>__attribute__ ((constructor))</code>  wouldn't work? This is with Linux 2.6.9, <code>gcc</code> version 3.4.6</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>For example, let's say the library code is this the following:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int smlib_count;
  
  void __attribute__ ((constructor)) setup(void) {
      smlib_count = 100;
      printf("smlib_count starting at %d\n", smlib_count);
  }
  
  void smlib_count_incr() {
      smlib_count++;
      smlib_count++;
  }
  
  int smlib_count_get() {
      return smlib_count;
  }
  </code></pre>
  
  <p>For building the .so I do the following:</p>
  
  <pre><code>gcc -fPIC -c smlib.c
  ld -shared -soname libsmlib.so.1 -o libsmlib.so.1.0 -lc smlib.o
  ldconfig -v -n .
  ln -sf libsmlib.so.1 libsmlib.so
  </code></pre>
  
  <p>Since the .so is not in one of the standard locations I update the <code>LD_LIBRARY_PATH</code> and link the .so from a another program. The constructor doesn't get called. If I change it to <code>_init()</code>, it works.</p>