本文介绍了使用G ++进行MPI代码时出现链接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码很简单:

#include <mpi.h>
int main(int argc, char**args) {
    MPI_Init(&argc, &args);
    int mpiSize;
    int mpiRank;
    MPI_Comm_size(MPI_COMM_WORLD, &mpiSize);
    MPI_Comm_rank(MPI_COMM_WORLD, &mpiRank);
    MPI_Finalize();
}

我首先将其编译为目标文件:

I first compile it to object file:

g++ -c src/mpitest.cpp -o src/mpitest.o

然后我可以轻松使用mpicxx:

Then I can easily use mpicxx:

mpicxx src/mpitest.o -o mpi

但是我想改用g ++,因为它更容易进行自动制作,所以我尝试了:

But I want to use g++ instead, because It's easier for automake, so I tried:

mpicxx src/mpitest.o -o mpi -show

它打印出来:

g++ src/mpitest.o -o mpi -I/usr/local/include -L/usr/local/lib -L/usr/local/lib -lmpichcxx -lmpich -lopa -lpthread -lrt

是的,该命令实际上成功完成了相同的操作.但是,如果我尝试将其更改为(我只是更改了目标文件并输出到末尾):

And yes, that command actually does the same thing successfully. However, If I tried to change it to (I just change the object file and output to the end):

g++ -I/usr/local/include -L/usr/local/lib -L/usr/local/lib -lmpichcxx -lmpich -lopa -lpthread -lrt src/mpitest.o -o mpi

当添加LDFLAGS时,automake会做什么,g ++开始抱怨...

Which is what automake does when it adds LDFLAGS, the g++ start complaining...

src/mpitest.o: In function `main':
..src/mpitest.cpp:5: undefined reference to `MPI_Init'
../src/mpitest.cpp:8: undefined reference to `MPI_Comm_size'
../src/mpitest.cpp:9: undefined reference to `MPI_Comm_rank'
../src/mpitest.cpp:10: undefined reference to `MPI_Finalize'
collect2: ld returned 1 exit status

g ++会发生什么?我不知道.请赐教.为什么这里的顺序很重要?从头开始做我想做的事情的正确方法是什么?

What happens with g++ ? I couldn't figure out. Please enlighten me. Why the order here matter at all ? What is the proper way to do what I want from the beginning ?

非常感谢

p/s:g ++-版本g ++(Ubuntu 4.4.3-4ubuntu5)4.4.3mpi是mvapich2

p/s : g++ --versiong++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3mpi is mvapich2

推荐答案

我真的不记得,也许从版本4开始,必须在所有源文件和目标文件之后传递与-l链接的库.

I didn't really remember, maybe since version 4, library to link with -l must be passed after all source and object files.

这篇关于使用G ++进行MPI代码时出现链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 10:19