简体   繁体   中英

Compile and Link C++ with 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

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

You can find the source files here: C++ and Fortran 77 .

Any idea what I can do to overcome this?

You have two main programs: one in C++ and one in Fortran. Delete the Fortran one either by

Option 1: The first bit of your Fortran program should be a BLOCK DATA segment. Just add

BLOCK DATA INFO

At the start.

Option 2: Delete everything up to the first END.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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