Oliver Spryn
Oliver Spryn

Reputation: 17368

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?

Upvotes: 0

Views: 1276

Answers (1)

cup
cup

Reputation: 8299

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.

Upvotes: 1

Related Questions