【发布时间】:2016-05-11 21:14:52
【问题描述】:
我遇到了一个编译时问题,我已将其简化为以下测试用例。我希望从 fortran 调用 C++ 例程并让 C++ 例程能够识别 MPI。
考虑以下示例代码,
Fortran 主程序:
! -- main.f90
program main
implicit none
external return_three
integer return_three
write(*,*) return_three()
end program main
C++ 子程序:
// -- subs.cpp
#include <mpi.h>
extern "C"
{
int return_three_();
}
int return_three_()
{
return 3;
}
请注意,为了重现问题,我只需要包含mpi.h。
使用 GCC 5.3 和 OpenMPI 1.10.1 进行编译(我也检查了 GCC 4.8 和 PGI 15.10)在链接过程中会出现以下问题:
% mpic++ -c subs.cpp
% mpifort -c main.f90
% mpifort -o main subs.o main.o -lstdc++ -lgcc_s
subs.o: In function `MPI::Intracomm::Intracomm()':
subs.cpp:(.text._ZN3MPI9IntracommC2Ev[_ZN3MPI9IntracommC5Ev]+0x14): undefined reference to `MPI::Comm::Comm()'
subs.o: In function `MPI::Intracomm::Intracomm(ompi_communicator_t*)':
subs.cpp:(.text._ZN3MPI9IntracommC2EP19ompi_communicator_t[_ZN3MPI9IntracommC5EP19ompi_communicator_t]+0x19): undefined reference to `MPI::Comm::Comm()'
subs.o: In function `MPI::Op::Init(void (*)(void const*, void*, int, MPI::Datatype const&), bool)':
subs.cpp:(.text._ZN3MPI2Op4InitEPFvPKvPviRKNS_8DatatypeEEb[_ZN3MPI2Op4InitEPFvPKvPviRKNS_8DatatypeEEb]+0x24): undefined reference to `ompi_mpi_cxx_op_intercept'
subs.o:(.rodata._ZTVN3MPI3WinE[_ZTVN3MPI3WinE]+0x48): undefined reference to `MPI::Win::Free()'
subs.o:(.rodata._ZTVN3MPI8DatatypeE[_ZTVN3MPI8DatatypeE]+0x78): undefined reference to `MPI::Datatype::Free()'
collect2: error: ld returned 1 exit status
在我看来,mpifort 缺少一些与 C++ 相关的库。不过,我的理解是 mpifort 应该用于编译 fortran 主程序。针对 OpenMPI 1.10.1 编译的 Intel 16.0 不会出现此问题。
我的问题是:
- 这里发生了什么?为什么英特尔能够处理此示例代码而 PGI/GCC 却不行?
- 是否有一种可移植的方式在 fortran 代码中包含带有 MPI 的 C++ 子例程?
- (如果可能)是否有一种简单的方法可以解决我当前的问题?我正在尝试在我的机器上编译一个包,所以最好添加
-lmagicfix或其他内容。
【问题讨论】: