简体   繁体   English

使用Fortran编译和链接C ++

[英]Compile and Link C++ with Fortran

I am trying to compile a valid Fortran 77 file using the gfortran compiler. 我正在尝试使用gfortran编译器编译有效的Fortran 77文件。 I use the following command to compile the Fortran: 我使用以下命令来编译Fortran:

gfortran -c main.f -o main-fortran.o -llapack -lblas

I also need to link this with a C++ program, which is compiled, like so: 我还需要将此链接与已编译的C ++程序进行链接,如下所示:

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

You can find the source files here: C++ and Fortran 77 . 您可以在此处找到源文件: C ++Fortran 77

Any idea what I can do to overcome this? 知道我能做些什么来克服这个问题吗?

You have two main programs: one in C++ and one in Fortran. 您有两个主要程序:一个在C ++中,一个在Fortran中。 Delete the Fortran one either by 删除Fortran之一

Option 1: The first bit of your Fortran program should be a BLOCK DATA segment. 选项1:Fortran程序的第一位应该是BLOCK DATA段。 Just add 只需添加

BLOCK DATA INFO

At the start. 在开始时。

Option 2: Delete everything up to the first END. 选项2:删除所有内容,直到第一个END。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM