Linking fails using mpicc on C++ program
Asked Answered
P

2

7

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:

#include <math.h>
#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;
}

From a terminal, I can use the mpicc wrapper to retrieve the follow information that I need for compiling and linking on my machine:

$ 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

I can compile the code successfully. However, the linking fails. Here is my compiler and linker steps:

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()'
.......

I used ....... 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.

My suspicion is that my linker flags are not in the correct order. Another thread comes to that conclusion: Link errors while using G++ for MPI code. But if so, I can't figure out what is out of order.

Why is my linking failing?

Polytypic answered 7/12, 2012 at 2:42 Comment(0)
P
19

SOLVED: I changed mpicc to mpic++, and now it works.

Polytypic answered 7/12, 2012 at 3:4 Comment(0)
H
1

Is your file a .c file or .cpp file. The question is mpicc is c compiler and mpic++ is c++ compiler, so if yours is a c program, compiling it with C++ compiler is not a recommendable I think.

Hermaphrodite answered 13/2, 2015 at 12:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.