zerospiel
zerospiel

Reputation: 642

OpenMPI compilation error

I have some *.cpp file which using OpenMPI. Also I have *.h file with specified to me functions (some of them) and implementation of them:

void Show(std::string s);
void ShowLine(std::string s);
void MPI_Output();

So I use these functions in my *.cpp file for example:

/*...*/
MPI_Output();

And including this header:

#include "my_h.h"

Then I'm trying to compile my frogram with mpicc:

mpicc -w my_best_program.cpp

It fails while in compilation process with following message:

/tmp/icpcBCoxCA.o: In function `main':
MPIDebug9.cpp:(.text+0xa46): undefined reference to `ShowLine(std::string)'
MPIDebug9.cpp:(.text+0xa90): undefined reference to `MPI_Output()'
MPIDebug9.cpp:(.text+0xc0f): undefined reference to `Show(std::string)'
/tmp/icpcBCoxCA.o: In function `info_main(double)':
MPIDebug9.cpp:(.text+0xe49): undefined reference to `Show(std::string)'
/tmp/icpcBCoxCA.o: In function `info(double)':
MPIDebug9.cpp:(.text+0x104e): undefined reference to `ShowLine(std::string)'

In addition, some info:

mpicc --showme:compile
-I/usr/mpi/intel/openmpi-1.4.4/include -pthread

Is there any solution for my problem?

Upvotes: 1

Views: 312

Answers (1)

nothrow
nothrow

Reputation: 16178

You aren't having problem during compilation, but during linkage.

mpicc -w my_best_program.cpp my_cpp_file_with_showline.cpp is something you need.

Explanation: (simplified)

In .h file, there is only information "Hey, compiler, there exist some method that is called Show, returns void, and wants parameter of type std::string. When you are compiling cpp file that calls this function, be sure it passes it correct parameters, but it will be provided in another .cpp file. (But I'm not telling you in which one. Just look at all of them and find appropriate one)"

It does the same for all of the separate .cpp files (creating .o files - your my_best_program.cpp contains something like: "There is info_main(double) function, that has some code, and it needs to call Show(std::string) - called Unresolved reference".

The my_cpp_file_with_showline.cpp will create .o with "There is Show(std::string) method, that does something else".

And, during linkage (that is invisible for you at this moment), all of the "Unresolved references" are resolved - meaning, the info_main(double) will call Show(std::string) from different cpp file.

When you call some C++ compiler from command line with multiple cpps, it usually compiles them separately (each one has no knowledge about another, just function declarations in .h files) and then links (merges all the function calls and given functions together) into executable.

Upvotes: 2

Related Questions