13
$\begingroup$

First, the main reference is the thread Linking FORTRAN with Librarylink, which teaches how to make the link between a FORTRAN subroutine and a Mathematica code, by using Librarylink. This must done through a C code, from where the FORTRAN subroutine is called. The C code is, then, compiled via Mathematica.

I have studied the examples and tried them myself. My doubt is on how to load FORTRAN libraries, e.g. for using the FORTRAN command open within the subroutine. Whenever I try to call a subroutine that contains open, I read an error message on the screen. Read below my minimal example.

FORTRAN subroutine

The subroutine reads a,b,c and stores a+b+c into the variable sum. Additionally it open,reads and close file.dat.

    !fadd.f90
    subroutine add(a,b,c,sum)
          implicit none
          integer a,b,c,sum
          sum=a+b+c
          open(1,file='file.dat')
          read(1,*)
          close(1)
          return
    end subroutine

file.dat only contains the number 1, for the minimal example. This value is not used anywhere, for simplification. My actual code would use dat files to make complicated computations.

Calling the subroutine from C

//MMA.cc
//Link directly to fortran object file
#include "WolframLibrary.h"

DLLEXPORT mint WolframLibrary_getVersion(){
    return WolframLibraryVersion;}
DLLEXPORT int WolframLibrary_initialize(WolframLibraryData libData){
    return 0;}

//declare fortran subroutine
extern "C" { void add_(mint* a, mint* b, mint* c, mint* sum);} 

EXTERN_C DLLEXPORT int add(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res){
    mint I0 = MArgument_getInteger(Args[0]);
    mint I1 = MArgument_getInteger(Args[1]);
    mint I2 = MArgument_getInteger(Args[2]);
    mint sum;
    add_(&I0,&I1,&I2,&sum);//call fortran subroutine
    MArgument_setInteger(Res,sum);
    return LIBRARY_NO_ERROR;
}

Mathematica: create library and load library function

Needs["CCompilerDriver`"];
CreateLibrary[{"MMA.cc", "fadd.o"}, "myadd", "Debug" -> True, "TargetDirectory" -> "."]

funcadd = LibraryFunctionLoad["./myadd", "add", {Integer, Integer, Integer}, Integer]

Print[ funcadd[2,2,2] ]
Exit[]

ERROR (Running Mathematica 8 through Linux terminal)

(*   /optifi/Wolfram/Mathematica/8.0.4/SystemFiles/Kernel/Binaries/Linux-x86-64/MathKernel: symbol lookup error: (...)/myadd.so: undefined symbol: _gfortran_st_open   *)

For compiling the FORTRAN code, I have run on LINUX terminal

gfortran -c -fPIC fadd.f90

where -fPIC has been added as requested by an error message telling recompile with -fPIC.

$\endgroup$
2
  • 1
    $\begingroup$ Might be a stupid question but are you sure your fortran code executes without error when started from a shell without all the extra C and MMA code? I suspect your fortran environment is not set up and libgfortran.so is not found. $\endgroup$
    – Matariki
    Nov 29, 2013 at 20:50
  • $\begingroup$ I am 100% sure. Please, could you try these codes in your system and tell me the error that you get? $\endgroup$
    – fcpenha
    Nov 30, 2013 at 23:10

1 Answer 1

12
$\begingroup$

Mathematicas invocation of the compiler doesn't know about where to find the Fortran library. With a little help, however, we can point the way. Mind you this was done on a Mac but the Linux variant of Unix will behave similar.

Needs["CCompilerDriver`"];
CreateLibrary[{"MMA.cc", "fadd.o"}, "myadd", 
 "Debug" -> True, 
 "TargetDirectory" -> ".", 
 LibraryDirectories -> "/usr/local/Cellar/gfortran/4.8.1/gfortran/lib/", 
 Libraries -> "gfortran"]

You need to put the directory to your gcc installations lib directory into the LibraryDirectory option and Mathematica will find the library. This was not necessary in the original code you were starting from as the Fortran commands used in the source code were not library function and hence no linking against libgfortran was required.

Be aware that the truncated name of the library is used with the Libraries option. The librarys full name, in my case, is libgfortran.dylib which gets truncated to gfortran.

$\endgroup$
2
  • $\begingroup$ With my actual code, I had some hard time finding the path to some specific libraries, but in the end it worked. $\endgroup$
    – fcpenha
    Dec 2, 2013 at 17:08
  • $\begingroup$ @fcpenha Good to hear that it worked out for you. $\endgroup$
    – Matariki
    Dec 2, 2013 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.