title: Why do I get undefined reference with the same link line?
body: <p>I am currently compiling two programs.</p>
  
  <p>The same link line produces no error on one, and a whole lot of undefined references on the other one.</p>
  
  <p>The link line is:</p>
  
  <pre><code>/usr/bin/c++   -Wall -pedantic -std=c++11   -fopenmp -O3 -DNDEBUG  
  CMakeFiles/program.dir/src/main.cpp.o  -o program -rdynamic 
  /home/felix/Development/local/lib/volop/libvolop.so 
  /home/felix/Development/local/lib/ghost/libghost.so 
  /home/felix/Development/local/lib/libhwloc.so 
  /usr/local/lib/libmpicxx.so 
  /usr/local/lib/libmpi.so -lrt -lpthread -L/usr/lib -lgsl -lgslcblas -lm 
  /home/felix/Development/PRIMME/libprimme.a 
  -L/usr/lib -lgsl -lgslcblas -lm -llapack 
  -L/usr/lib -lgsl -lgslcblas -lm /home/felix/libblas/lib/libopenblas.so 
  /home/felix/Development/PRIMME/libprimme.a 
  -llapack 
  /home/felix/libblas/lib/libopenblas.so 
  -Wl,-rpath,/home/felix/Development/local/lib/volop:/home/felix/Development/local/lib/ghost:/home/felix/Development/local/lib:/usr/local/lib:/home/felix/libblas/lib 
  </code></pre>
  
  <p>And the associated error:</p>
  
  <pre><code>/home/felix/Development/local/lib/ghost/libghost.so: undefined reference to `MPI_Allgather'
  /home/felix/Development/local/lib/ghost/libghost.so: undefined reference to `MPI_Init_thread'
  </code></pre>
  
  <p>and so on, all MPI Linker errors. However as you can see from the link line mpi is linked in. And in the other program it works just fine. </p>
  
  <p>This is on the same computer with the same compiler. The link line is generated by cmake. The CMakeLists.txt is identical for both programs.</p>
  
  <p>I stop getting linker errors once I start using MPI Methods explicitely in the program which produces the errors. Why?</p>
  
  <p>EDIT: As requested in the comments</p>
  
  <pre><code>ldd /home/felix/Development/local/lib/ghost/libghost.so
  linux-vdso.so.1 =&gt;  (0x00007fff6dff8000)
  libstdc++.so.6 =&gt; /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f8d70837000)
  libm.so.6 =&gt; /lib/x86_64-linux-gnu/libm.so.6 (0x00007f8d70531000)
  libgomp.so.1 =&gt; /usr/lib/x86_64-linux-gnu/libgomp.so.1 (0x00007f8d70321000)
  libgcc_s.so.1 =&gt; /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f8d7010b000)
  libpthread.so.0 =&gt; /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f8d6feed000)
  libc.so.6 =&gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007f8d6fb26000)
  /lib64/ld-linux-x86-64.so.2 (0x00007f8d70f81000)
  </code></pre>