4
votes

I've got a Fortran subroutine that I would like to call from a C++ program. It takes a long list of floating-point arguments and uses the iso_c_binding intrinsic module:

subroutine parasolve ( ...... ) bind (c, name='c_parasolve')
  use,intrinsic :: iso_c_binding
  implicit none
....

Based on what I've read, I understand that I need to use C++'s "extern" command to define the external function before calling it later. I tried it two ways. The first:

extern "C" void c_parasolve( .... );

returns "expected unqualified-id before string constant" at compile time, whereas the second:

extern void c_parasolve( .... );

compiles just fine but fails to link with "undefined reference to 'c_parasolve( .... )'" and ld returns 1.

I'm compiling with:

g++ -c main.cpp

etc, and

gfortran -ffree-form -std=f2003 -c parasolve.f03

to get them into .o ELFs and then attempting to link with:

g++ main.o otherfiles.o parasolve.o -lgfortran

What is the proper way to call this Fortran function?

1
What are your command to compile and link this code? - M. S. B.
I've edited it to show the commands - nathanvy
so i think you are getting the above error at first line.that is G++ -c main.cpp ???? - Ali786
The undefined reference error occurs when linking. The "expected unqualified-id before string constant" occurs when compiling main.cpp with g++. The fortran code compiles properly with gfortran. - nathanvy
try this g++ -c -std=c++0x -std=gnu++0x main.cpp -lgfortran - Ali786

1 Answers

5
votes

It looks like your extern "C" declaration is inside a function or class definition. This is not allowed -- it has to be at the top level in the source file. (Yes, the error message could be more informative!)