0

My Fortran code needs to call C++ function named SolveBIE_(), and this function is written in one file, e.g. test.c. SolveBIE_() needs to call some other C++ functions written in other files, e.g. part01.c, part02.c. I also claim those functions in part01.h and part02.h. Then I use

 $ gcc -c part01.c
 $ gcc -c part02.c
 $ ar cr kf.a part01.o part02.o  part01.h part02.h

to get my kf.a. I link the kf.a as a object file in my fortran compiling. I am using intel fortran ifort and a makefile. I got an error in my make. Here is the makefile script.

FC =  ifort
IFC_OPT = -O3 -ipo -unroll
FFLAGS = -extend_source -mp1 -xW -w95 -cm $(IFC_OPT) -cm
LINKFLAGS = -LFFTPACK  -ldfftpack 
CC = cc
HS_OBJECTS = xx.o kf.a ...\ objectfile
VEL  = vel_new_massflux.o matvec.o 
MAIN = xx.o new_restart.o
hsflux_objs = $(MAIN) $(VEL) $(HS_OBJECTS)
hsflux: $(hsflux_objs)
$(FC) -o hsflux $(FFLAGS) $(hsflux_objs) $(LINKFLAGS)

hsfluxd_objs = driver_massflux_debug.o $(VEL) $(HS_OBJECTS)
hsflux_debug: $(hsfluxd_objs)
$(FC) -o hsflux_debug $(FFLAGS) $(hsfluxd_objs) $(LINKFLAGS)

$(FC) $(FFLAGS) -o testfft2 testfft2.o FAST.o xfft.o $(LINKFLAGS)

  .f.o:
$(FC) -c $(FFLAGS) $*.f

   .c.o:
$(CC) -c $*.c

   clean:
/bin/rm -f *.o *~

   VISCFING_FILES=Makefile *.f *.F *.c *.h  FFTPACK Scripts PARAMS

The error is

  ipo: warning #11021: unresolved SolverBIE_
      Referenced in  /tmp/ipo_ifortUqyTSb.o
 /tmp/ipo_ifortUqyTSb.o: In function 'MAIN_':
 ipo_out.f:(.text+0x5442): undefined reference to 'SolveBIE_'

The detail of the C++ code and Fortran code is here, another question I asked, it seems that problem is solved. But I need to complete the compiling to make sure. C++ code and fortran code

Yue
  • 23
  • 6
  • Have you seen this QA? https://stackoverflow.com/questions/17845931/calling-c-function-subroutine-in-fortran-code – Dai Feb 08 '18 at 23:59
  • Also, if the `SolveBIE__` function is in a file called `test.c` then it probably isn't C++, but just C. Exporting C++ code (e.g. member functions) and calling them from outside C++ is more difficult than C functions (name-mangling, vtables, etc). – Dai Feb 09 '18 at 00:00
  • Thanks for the comment. The page is little useful. I haven't seen the solution to the problem. – Yue Feb 09 '18 at 17:10

1 Answers1

0

Going back to your linked code, the Fortran has

  integer (C_INT) function SolveBIE_(x, y, aa, m) BIND(C, NAME='SolveBIE_')

Whereas the C has

  int solveBIE_(double *ini_bdry_x, double *ini_bdry_y, double *ini_bdry_um, int *fM)

The Fortran part is thus looking for a symbol called SolveBIE_ while the C part is providing a symbol called solveBIE_. The difference in case of every character matters!

user5713492
  • 954
  • 5
  • 11
  • On this forum I have observed that you get downvoted every time you correctly answer the posted question. Thanks to the downvoter for its confidence in my answer. – user5713492 Feb 13 '18 at 17:05
  • The error disappeared. But I have some other errors. It is almostly about the undefined reference. Is there any tutorial about how to make mixture languages compiling? – Yue Feb 14 '18 at 17:05
  • When you say the error disappeared do you mean that you fixed either the Fortran code or the C++ code so that both sides agreed on the name for the function? Fortran has an automatic way to ensure that when you call another procedure that both sides agree on the interface, through modules. C achieves the same thing through header files, but there isn't an automatic way to get it right across different languages. In Fortran you use the C interoperability features and in C++ declare all as extern "C" but you have to make sure you get it right, especially levels of indirection. – user5713492 Feb 14 '18 at 17:14