【问题标题】:OpenMPI compilation errorOpenMPI 编译错误
【发布时间】:2013-11-18 19:46:47
【问题描述】:

我有一些使用 OpenMPI 的 *.cpp 文件。我也有 *.h 文件,其中指定了我的功能(其中一些)和它们的实现:

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

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

/*...*/
MPI_Output();

包括这个标题:

#include "my_h.h"

然后我尝试用mpicc 编译我的frogram:

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

我的问题有什么解决办法吗?

【问题讨论】:

    标签: c++ compilation compiler-errors mpi openmpi


    【解决方案1】:

    在编译过程中没有问题,但在链接过程中。

    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 将使用“有 Show(std::string) 方法,它可以做其他事情”来创建 .o。

    并且,在链接期间(此时您不可见),所有“未解析的引用”都已解析 - 这意味着 info_main(double) 将从不同的 cpp 文件中调用 Show(std::string)

    当你从带有多个 cpps 的命令行调用某些 C++ 编译器时,它通常会分别编译它们(每个都不知道另一个,只是 .h 文件中的函数声明)然后链接(合并所有函数调用和给定函数一起)成可执行文件。

    【讨论】:

    • 我需要指定 *.cpp 文件来实现我的功能吗? UPD 好的,我用你的解决方案成功编译了我的程序。
    猜你喜欢
    • 2013-07-07
    • 1970-01-01
    • 2011-10-31
    • 1970-01-01
    • 2019-10-27
    • 2017-10-24
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多