title: Linking fails using mpicc on C++ program
body: <p>I'm running Ubuntu 11.04 64-bit.  I have installed OpenMPI.  I'm trying to build the following code, which is a snippet from a test problem from the book "Using MPI" by Gropp/Lusk/Skjellum:</p>
  
  <pre><code>#include &lt;math.h&gt;
  #include "mpi.h"
  
  int main(int argc, char *argv[])
  {
  int size, rank;
  
      MPI::Init(argc, argv);
  
      size = MPI::COMM_WORLD.Get_size();
      rank = MPI::COMM_WORLD.Get_rank();
  
      MPI::Finalize();
      return 0;
  }
  </code></pre>
  
  <p>From a terminal, I can use the mpicc wrapper to retrieve the follow information that I need for compiling and linking on my machine:</p>
  
  <pre><code>$ mpicc --showme:compile
  -I/usr/lib/openmpi/include -I/usr/lib/openmpi/include/openmpi -pthread
  $ mpicc --showme:link
  -pthread -L/usr/lib/openmpi/lib -lmpi -lopen-rte -lopen-pal -ldl -Wl,--export-dynamic -lnsl -lutil -lm -ldl
  </code></pre>
  
  <p>I can compile the code successfully.  However, the <strong>linking fails</strong>.  Here is my compiler and linker steps:</p>
  
  <pre><code>make all 
  Building file: ../main.cpp
  Invoking: GCC C++ Compiler
  mpicc -I/usr/lib/openmpi/include -I/usr/lib/openmpi/include/openmpi -O0 -g3 -Wall -c -fmessage-length=0 -pthread -MMD -MP -MF"main.d" -MT"main.d" -o"main.o" "../main.cpp"
  Finished building: ../main.cpp
  
  Building target: test
  Invoking: GCC C++ Linker
  mpicc -L/usr/lib/openmpi/lib -pthread -Wl,--export-dynamic -o"test"  ./main.o   -lm -ldl -lmpi -lopen-rte -lopen-pal -lnsl -lutil
  ./main.o: In function `main':
  /home/djunderw/ncsu/workspace/test/Debug/../main.cpp:20: undefined reference to `MPI::COMM_WORLD'
  /home/djunderw/ncsu/workspace/test/Debug/../main.cpp:21: undefined reference to `MPI::COMM_WORLD'
  ./main.o: In function `__static_initialization_and_destruction_0':
  /usr/include/c++/4.4/iostream:72: undefined reference to `std::ios_base::Init::Init()'
  /usr/include/c++/4.4/iostream:72: undefined reference to `std::ios_base::Init::~Init()'
  .......
  </code></pre>
  
  <p>I used <code>.......</code> above to denote the fact that the error message go on and on--there are quite a few of them, but I don't think they're all necessary to show here.  If they are, please ask, and I'll paste them all.</p>
  
  <p><strong>My suspicion is that my linker flags are not in the correct order.</strong>  Another thread comes to that conclusion: <a href="https://stackoverflow.com/questions/6451340/link-errors-while-using-g-for-mpi-code">Link errors while using G++ for MPI code</a>.  But if so, I can't figure out what is out of order.</p>
  
  <p><strong>Why is my linking failing?</strong></p>