title: Using C++11 multithreading in shared library loaded by program without thread support
body: <p>I am currently trying to use C++11 multithreading in a shared library that is loaded into the main program (written in C) on Linux. This is part of a big simulation program and I cannot change anything about the library loading or change the main program in general.</p>
  
  <p>The main program is compiled with gcc 4.1.2 and I don't have the sources for it (I cannot recompile it with gcc 4.8.2).</p>
  
  <p>The shared library is compiled with gcc 4.8.2 in order to use C++11 multithreading. I am passing the compiler commands</p>
  
  <pre class="lang-none prettyprint-override"><code>-pthread -lpthread -std=c++11
  </code></pre>
  
  <p>as explained in <a href="https://stackoverflow.com/questions/8649828/what-is-the-correct-link-options-to-use-stdthread-in-gcc-under-linux">What is the correct link options to use std::thread in GCC under linux?</a></p>
  
  <p>Compiling a standalone test program with this configuration ("<code>-pthread -std=c++11</code>" and gcc 4.8) works correctly on my system. But when I start the program loading the shared library I get an exception:</p>
  
  <pre class="lang-none prettyprint-override"><code>Caught std::exception!
  Exception Message: Enable multithreading to use std::thread: Operation not permitted
  
  Terminating...
  </code></pre>
  
  <p>Using the <code>-pthread</code> and <code>-lpthread</code> (<strong>Edit:</strong> and also only <code>-pthread</code> without <code>-lpthread</code>) compile parameter doesn't work.
  The compiler arguments are (I am using the cook build system):</p>
  
  <pre class="lang-none prettyprint-override"><code>-pthread -std=c++11 -fmessage-length=0 -fPIC -Wchar-subscripts ...(lots of -W* here)
  ... -Wunused-variable -m64 -D__64BIT__ -pthread -lpthread
  </code></pre>
  
  <p>and the linker arguments (duplicate parameters due to the build system):</p>
  
  <pre class="lang-none prettyprint-override"><code>-pthread -lpthread -std=c++11 -pthread -lpthread -std=c++11 -shared -fPIC -Wl,-Bsymbolic -Wl,--allow-shlib-undefined -pthread -lpthread
  </code></pre>
  
  <p>calling ldd on my library gives the following output</p>
  
  <pre class="lang-none prettyprint-override"><code>$ ldd calc3/build/amd64_linux26_RH5/library.so
      linux-vdso.so.1 =&gt;  (0x00007fff4d1fd000)
      libpthread.so.0 =&gt; /lib64/libpthread.so.0 (0x00002ae6ec124000)
      libstdc++.so.6 =&gt; /afs/bb/data/d6833/util/gcc_482/lib64/libstdc++.so.6 (0x00002ae6ec340000)
      libm.so.6 =&gt; /lib64/libm.so.6 (0x00002ae6ec655000)
      libgcc_s.so.1 =&gt; /afs/bb/data/d6833/util/gcc_482/lib64/libgcc_s.so.1 (0x00002ae6ec8d8000)
      libc.so.6 =&gt; /lib64/libc.so.6 (0x00002ae6ecaef000)
      /lib64/ld-linux-x86-64.so.2 (0x00000032cb400000)
  </code></pre>
  
  <p>and on the main program</p>
  
  <pre class="lang-none prettyprint-override"><code>$ ldd .../bin-64/main_program
      linux-vdso.so.1 =&gt;  (0x00007fff64595000)
      libdl.so.2 =&gt; /lib64/libdl.so.2 (0x00000032cc000000)
      libz.so.1 =&gt; /usr/lib64/libz.so.1 (0x00000032cc800000)
      libc.so.6 =&gt; /lib64/libc.so.6 (0x00000032cb800000)
      /lib64/ld-linux-x86-64.so.2 (0x00000032cb400000)
  </code></pre>
  
  <p>The pthread library is linked to my shared library but not to the main program.
  This <a href="https://stackoverflow.com/a/9689770/3099216">answer</a> states that you have to link pthreads to the main program but the 2nd comment on this answer (by @R..) says it isn't necessary (which sounds logically).</p>
  
  <p>Unfortunately I don't know anything about the loading mechanics of the whole system, except that my library is using another C++ library as API.</p>
  
  <p>Note that other C++11 features do work (and libstdc++.so is in the dependencies of my library) but C++11 multithreading is not (although libpthread.so is also in the dependencies of my library).</p>
  
  <p>Using a threading class from a library contained in the program itself is working (and this thread class seems to use pthreads too).</p>
  
  <p>I have also tried to use <code>-fabi-version=0</code> or <code>-fabi-version=2</code> because the main program is compiled with gcc 4.1.2 with my library but it didn't change anything.</p>
  
  <p>Is there anything I have overlooked or a compiler option I can use to make it work? Or does it seem to be a problem of my program environment? Any ideas are welcome.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I tried using <code>-Wl,-no-as-needed</code> (as suggested in the comments) but it unfortunately didn't change anything.</p>
  
  <p>Using clang 3.5 instead of gcc 4.8 also didn't work.</p>
  
  <p>Creating a small test application which loads a shared library (like in the answer below by @chill) works (even without the compiler flag) as long as I use gcc 4.8 or clang 3.5 for both main application and shared library. When using gcc 4.1 for the main program however the main program even fails to load the library (which is working in my 'real' application). I think there might be a problem with the differen ABIs of the compilers.</p>
  
  <p>Using pthreads directly from <code>pthread.h</code> seems to work (although the program currently terminates on <code>pthread_join</code> without error message, but I'm still testing there...)</p>
  
  <p><strong>Edit 2:</strong></p>
  
  <p>Running the 'test program' with <code>LD_LIBRARY_PATH=$(pwd):$LD_LIBRARY_PATH</code> (because the gcc 4.8 library paths need to be in there too, thanks @MvG) did run the program but crashed again with the <code>Enable multithreading to use std::thread: Operation not permitted</code> exception.</p>
  
  <p>I checked all other libraries that are loaded (found them with <code>strace ./main_program 2&gt;&amp;1 | grep '^open(".*\.so"'</code>  [see <a href="https://stackoverflow.com/a/5103749/3099216">here</a>]) and checked them all with <code>ldd</code>. They all depend on the same libraries (with the same paths). <code>ldd</code> outputs (on all of them):</p>
  
  <pre class="lang-none prettyprint-override"><code>linux-vdso.so.1 =&gt;  (0x00007fff4d3fd000)
  libstdc++.so.6 =&gt; /afs/bb/data/d6833/util/gcc_482/lib64/libstdc++.so.6 (0x00002ade28774000)
  libm.so.6 =&gt; /lib64/libm.so.6 (0x00002ade28ab0000)
  libgcc_s.so.1 =&gt; /afs/bb/data/d6833/util/gcc_482/lib64/libgcc_s.so.1 (0x00002ade28d33000)
  libc.so.6 =&gt; /lib64/libc.so.6 (0x00002ade28f49000)
  /lib64/ld-linux-x86-64.so.2 (0x00000032ea200000)
  </code></pre>
  
  <p>(that they all don't depend on libpthread.so.0 except my library and one other (but it's the same <code>/lib64/libpthread.so.0</code>))</p>
  
  <p>Some libraries do have more dependencies (which don't seem thread-related) but there don't seem to be any 'conflicting' dependencies (there are no dependencies to different versions/paths of the same library in any of these libraries).</p>