OpenMPI编译错误

OpenMPI compilation error

本文关键字:错误 编译 OpenMPI      更新时间:2023-10-16

我有一些使用OpenMPI的*.cpp文件。此外,我还有*.h文件,其中包含指定给我的函数(其中一些)和它们的实现:

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

所以我在*.cpp文件中使用这些函数,例如:

/*...*/
MPI_Output();

包括这个标题:

#include "my_h.h"

然后我尝试用mpicc:编译我的蛙图

mpicc -w my_best_program.cpp

它在编译过程中失败,并显示以下消息:

/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)'

此外,一些信息:

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

我的问题有解决办法吗?

您在编译过程中没有遇到问题,但在链接过程中遇到了问题。

mpicc -w my_best_program.cpp my_cpp_file_with_showline.cpp是你需要的东西。

说明:(简化)

在.h文件中,只有信息"嘿,编译器,有一些方法叫Show,返回void,并且想要std::string类型的参数。当你编译调用这个函数的cpp文件时,请确保它传递了正确的参数,但它会在另一个.cpp文件中提供

它对所有单独的.cpp文件都是一样的(创建.o文件-您的my_best_program.cpp包含类似于:"有一个info_main(double)函数,它有一些代码,它需要调用Show(std::string)-称为未解析引用"

my_cpp_file_with_showline.cpp将使用"There is Show(std::string) method,that does other something"创建.o。

而且,在链接过程中(目前对您来说是不可见的),所有"未解析的引用"都会被解析——这意味着info_main(double)将从不同的cpp文件调用Show(std::string)

当您使用多个cpp从命令行调用某些C++编译器时,它通常会单独编译它们(每个cpp对另一个cpp一无所知,只是.h文件中的函数声明),然后将所有函数调用和给定函数链接(合并在一起)为可执行文件。