使用Fortran编译和链接C ++ [英] Compile and Link C++ with Fortran
问题描述
我正在尝试使用 gfortran
编译器来编译有效的Fortran 77文件.我使用以下命令来编译Fortran:
I am trying to compile a valid Fortran 77 file using the gfortran
compiler. I use the following command to compile the Fortran:
gfortran -c main.f -o main-fortran.o -llapack -lblas
我还需要将此链接与已编译的C ++程序链接,如下所示:
I also need to link this with a C++ program, which is compiled, like so:
g++ -c main.cpp -o main-cpp.o
这两个文件都可以编译,没有任何错误或警告.
Both of these files compile without any errors or warnings.
但是,我无法将它们链接到一个可执行文件中.这是我正在使用的命令:
However, I cannot link them together into an executable. Here is the command I am using:
g++ main-cpp.o main-fortran.o -o run.exe -lgfortran -llapack -lblas
链接器然后给出此错误:
The linker then gives this error:
main-fortran.o: In function `main':
main.f:(.text+0x2e18): multiple definition of `main'
main-cpp.o:main.cpp:(.text+0x8e9): first defined here
collect2: ld returned 1 exit status
您可以在以下位置找到源文件: C ++ 和 Fortran 77 .
You can find the source files here: C++ and Fortran 77.
有什么办法可以克服这个问题吗?
Any idea what I can do to overcome this?
推荐答案
您有两个主要程序:一个在C ++中,一个在Fortran中.通过以下方式删除Fortran之一
You have two main programs: one in C++ and one in Fortran. Delete the Fortran one either by
选项1:Fortran程序的第一位应该是BLOCK DATA段.只需添加
Option 1: The first bit of your Fortran program should be a BLOCK DATA segment. Just add
BLOCK DATA INFO
在一开始.
选项2:删除所有内容,直到第一个END.
Option 2: Delete everything up to the first END.
这篇关于使用Fortran编译和链接C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!