1

I am going to call a C++ function from FORTRAN, for which I am using ISO_C_Binding module. After compaction of the FORTRAN main file and C++ function with commands

 gfortran -c mlp8.f90

 g++ -c mean_cpp.cc

Which will create the objects files but in the linking phase as suggested by some members I am going to use the commands

 g++ mlp8.o mean_cpp.o -o main –lgfortran

I.e. using C++ compiler with linking to FORTRAN libraries but it gives error like

 /Cygnus/cygwin-b20/H-i586-cygwin32/i586-win32/bin/ld: 
   cannot open –lgfortran: No such a file or directory 

 Collect2:ld return 1 exit status 

So I think the main problem is that the g++ linker can not link with the FORTRAN libraries, so may be I need to include some path in the linking option or may be I need to do some setting in the g++ complier, which I don’t know how to do this, so please help to sort out this problem.

osgx
  • 90,338
  • 53
  • 357
  • 513
Zahur
  • 101
  • 1
  • 3
  • 9
  • The linker can't find the gfortran library (filename is libgfortran.a or libgfortran.so). Can you check if this file is present in your /lib directory? Also, I'd rearrange the command line to put the .o files behind -o main. – fvu Aug 31 '11 at 23:19
  • 2
    This is virtually the same question as http://stackoverflow.com/questions/7255468/calling-c-function-from-fortran-not-c, asked earlier today. It would be better to merge them. – M. S. B. Sep 01 '11 at 00:38

1 Answers1

3

You should find file libgfortran.* (e.g. with locate of find / -name "libgfortran.*"; or in windows-way Win+g, F3 or any file manager), record the path where it is and do

 g++ mlp8.o mean_cpp.o -o main -LPATH_RECORDED –lgfortran

where PATH_RECORDED is the path.

Try this lib list (got it from my mingw gfortran with -v option)

 g++ mlp8.o mean_cpp.o -o main -LPATH_RECORDED –lgfortran -lmingw32 -lgcc_s -lgcc -lmoldname  -lmingwex -lmsvcrt
osgx
  • 90,338
  • 53
  • 357
  • 513
  • I use the command (g++ main.o print_hi.o -o main -LC:\gfortran\lib –lgfortran) for linking, in which C:\glfortan\lib is the path to libgfortran.a file but it gives a lot of errors such as c:\gfortran\lib/libgfotran.a(transfer.o)(.text$sprintf+0x1b): transfer.c undefined reference to ‘_ _mingw_vsprintf’ Etc etc What I should do to overcome this problem, I also try some linking with gfortran with address to C++ libraries but again gives a lot of errors. I am using windows Vista, with g++ and gfortran compilers. Thanks in advance – Zahur Sep 01 '11 at 10:42
  • Thank you very much for your prompt reply, which is very helpful. Now the problem is that if I use the command (g++ main.o print_hi.o -o main -LC:\gfortran\lib -lgfortran -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt) suggested by you so it gives error (/Cygnus/cygwin-b20/H-i586-cygwin32/i586-cygwin32/bin/1d: cannot open –lgcc_s: No such a file or directory) So it can not find this library file, which in not there in both gfortran or in g++ folders, even I searched the whole computer but cannot find such a file. – Zahur Sep 01 '11 at 11:42
  • When I use the command with out this -lgcc_s option then it create the main.exe file but when I execute the exe it gives [main] C:\aaa\main.exe 1000(0) handle_exceptions: Exception: STATUS_STACK OVERFLoW [mian]mian 1000 (0) handle_exception: Dumping stack trace to main.exe.core And generate a file name main.exe.core – Zahur Sep 01 '11 at 11:43
  • Sorry, my command is for mingw compilers. Are your g++ from cygwin or mingw? And what about gfortran? – osgx Sep 01 '11 at 12:42
  • Thank you very much for pointing out this, I remove the old version of gfortran and g++ compiler, which I don’t remember exactly from where I download and install the MinGW one, the detail installation procedure is given at http://www.mingw.org/wiki/Getting_Started. I use the command line interface installer. Which is now working absolutely fine, I tried both call from FORTRAN to C++ and vice versa, even I tried the both command as gfortran main.o print_hi.o -o main -lstdc++ g++ main.o print_hi.o -o main –lgfortran Thanks again – Zahur Sep 01 '11 at 14:41
  • So, when your question is solved, you can accept an answer. At left side of answer, just under "Vote counter" with buttons `^` and `v` there is an `v` symbol - click it. – osgx Sep 01 '11 at 15:17