tags:

views:

685

answers:

3

MPICH2 is installed in C:\Program Files\MPICH2. There are two subdirectories (of interest), \include which contains .h files, and \lib which contains .lib files.

The readme that comes with MPICH2 has the following instructions:

  1. create a makefile
  2. add –I…mpich2\include (uppercase i)
  3. add –L…mpich2\lib
  4. add –lmpi (lowercase L)
  5. add the rules for your source files
  6. compile

Since there are no other rules in my project, I don't create a makefile, I just go to the command line and try compiling like this:

g++ -I"C:\Program Files\MPICH2\include" main.cpp -L"C:\Program Files\MPICH2\lib" -lmpi

This gives me a fistful of undefined reference errors on every single MPI symbol in the code. I spent hours trying to fix it, juggling -I, -L and -l switches around, shuffling the order of the parameters, even copied all the .lib files into the same directory as my source, but nothing seems to work.

What kind of voodoo is needed to get this thing to link?


EDIT: I think I found the problem: here's an excerpt of the linker's output in verbose mode (adding -Wl,--verbose to the compile command):

attempt to open C:\Program Files\MPICH2\lib/libmingwex.dll.a failed
attempt to open C:\Program Files\MPICH2\lib/mingwex.dll.a failed
attempt to open C:\Program Files\MPICH2\lib/libmingwex.a failed
attempt to open C:\Program Files\MPICH2\lib/mingwex.lib failed
attempt to open C:\Program Files\MPICH2\lib/libmingwex.dll failed
attempt to open C:\Program Files\MPICH2\lib/mingwex.dll failed
attempt to open C:\Program Files\MPICH2\lib\libmingwex.a failed

Apparently, the linker adds a / instead of a \ to the directory names I supply it with (except when looking for the lib___.a format for some reason), which is obviously not a valid path. Is there any way to tell the linker to use backslashes instead of slashes?

This also caught my eye:

attempt to open /mingw/lib/libmingwex.a succeeded

So I tried compiling like this:

g++ -I"/Program Files/MPICH2/include" -L"/Program Files/MPICH2/lib" objManager.cpp ongom.cpp io.cpp main.cpp -lmpi -lcxx

But I still get the same undefined reference errors.

A: 

try adding -lmpicxx (the lib for the c++ bindings), and make sure the -l... come after the cpp source file *. this works for me:

g++ -Iinclude -Llib test/cxxpi.cpp -lmpicxx -lmpi

EDIT: re: "undefined reference to 'MPI_Comm_rank'": could it be that your are mixing up / using c and / instead of c++? MPI_Comm_rank seems to be the c binding - the c++ binding would be MPI::Comm::Get_rank(). maybe try compiling your program as c, or, if you want to use c++, using the proper bindings (see cxxpi.cpp in the examples dir)?

* http://newsgroups.derkeiler.com/Archive/Comp/comp.parallel.mpi/2006-08/msg00036.html

ax
Still getting the same `undefined reference` errors, even if I add every library and put all the parameters exactly the same order as you wrote.
suszterpatt
did you read my edit re: c vs. c++ bindings? also, try using forward slashes, relative paths, and have no spaces in path names. eg. put your sources into "C:\Program Files\MPICH2\sc" and use -I../include -L../lib etc. in the end, it is working for me ...
ax
oh, and by the way: the linker looks for libmpi.a and (if it's c++) libmpicxx.a - that's why the -lmpicxx -lmpi.
ax
A: 

I had a similar issue with mingw: for those library files with a .lib ending, I had to put the name of the library without the ending (e.g. -llibboost_system-mgw34-mt when the filename is libbboost_system-mgw34-mt.lib). For library files with a .a ending, I had to put the name of the library excluding the starting "lib" and the trailing .a (e.g. -lws2_32 for libws2_32.a).

So in your case - try -llibmpi (or whatever your file is called without the .lib ending), perhaps it's the same issue.


from: http://www.mingw.org/node/98/revisions/358/view

Note: some paths were printed with “/” as the path separator while some other was printed with “\” as the path separator. I've substitued all with “/” as MinGW GCC accept both.

So I would not put too much time into finding a way to correct the path seperator. Is your library compiled for mingw?

perhaps: http://www.mingw.org/wiki/LibraryPathHOWTO helps you a bit further.

Tobias Langner
Apparently the linker tries a variety of formats, but creates the wrong paths: see my edit in the question.
suszterpatt
can you please tell the name of the mpi-libarary file? is it libmpi.a or libmpi.lib? This is important since if you want to link to the libmpi.lib file, you need -llibmpi instead of -lmpi
Tobias Langner
The files have no `lib` prefix, and an extension of `.lib`. e.g. `mpi.lib` and `cxx.lib`. Looking at the verbose output above, the option `-lmpi` should attempt to look for an `mpi.lib` (as per the 4th line in the excerpt).
suszterpatt
A: 

I'm sorry if this is an old thread, but I'm having the same error here. I installed MPICH2 and tried the following example in C:

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>

int main(int argc, char **argv)
{
  int ierr;
  ierr = MPI_Init(&argc, &argv);
  ierr = MPI_Finalize();
  return EXIT_SUCCESS;
}

It compiles without errors, and I'm linking using:

gcc -o main.o -L/C/bin/MPICH2/lib -lmpi

But I'm still getting undefined reference to 'MPI_Init' and undefined reference to 'MPI_Finalize' errors. When I tried with -Wl,--verbose I got success on every library file.

I can't figure out what's wrong with it! :(

mpcabd
Apparently the error has to do with x64 version of MPICH2, I removed it and installed IA32 version and it worked!Any idea what might be wrong with x64 version?
mpcabd
I have never got the x64 version to work. Try nm, I think it is the symbol decorations.
whatnick