Calling FORTRAN from Root/C++ Makefile question

Hi,

I am trying to compile with FORTRAN code that I got from a theorist. I tried following the HOWTO for doing this with CERNLIB and I make it as far as modifying the Makefile. The Makefile in the example looks NOTHING like the one on my linux machine. I am Makefile/compiler illiterate. How do I modify this for the linux Makefile that comes with v3.10/02 ?

I have source code for the fortran, not a library. If I need a library, how do I make that?
Is there an easier way if I am working from the fortran source, rather that a .a file?

Thanks,

Alex

Hello Alex,

I’m mixing Fortran and RooT for some time now and it works perfectly.
In the attachment you see a Makefile example for compiling and linking a shared library for a RooT class in wich Fortran subroutines are called.
Look for instance how the cernlibs are linked in. I think linking of g2c and f2c is always necessary.

No problem. Just link the object files of your Fortran code in. Create them with

g77 -c *.fpp

If you want to learn more about Makefiles, have a look at http://www.gnu.org/software/make/manual/make.html

Good luck!
Oliver
Makefile.txt (5.57 KB)

a simple example with a fortran file myf77.f

function myf77(arg1,arg2) integer arg1 double precision arg2,myf77 myf77 = arg1*sqrt(arg2) print *, 'arg1=',arg1,' arg2=',arg2, ' myf77=',myf77 end
Create a small shared lib

g77 -c myf77.f g++ -Wl,-soname,libmyf77.so -shared -o libmyf77.so myf77.o -lg2c
a simple C++ file myc2f77.C calling this f77 function

#define myf77 myf77_ extern "C" double myf77(int &arg1, double &arg2); void myc2f77(int arg1, double arg2) { double res = myf77(arg1,arg2); printf("res=%g\n",res); }
Now a simple ROOT session

root > gSystem->Load("libmyf77") root > .L myc2f77.C+ root > myc2f77(2,6)
Rene

Greetings all,

I am trying to work my way through calling FORTRAN from ROOT and figured that working through Rene’s simple example was the best starting point. The only deviations from Rene’s example were because of changes to some compiler syntax - but I followed it as closely as possible.

I have tried compiling the files using the following options

[quote]gfortran -c -fPIC myf77.f
g++ -Wl,-soname,libmyf77.so -shared -o libmyf77.so myf77.o
[/quote]
I could not use g77 as it has been deprecated in my Linux version. The switch -lg2c could not be used because it has been deprecated and the documentation says that it is unnecessary for gfortran. The switch -fPIC was necessary in order to get the object code to work with g++. The shared object compiled without a problem but when I started a root session and tried to follow the directions for invoking this I got the following

[quote]
root [0] gSystem->Load(“libmyf77”)
(int)0
root [1] .L myc2f77.C++
Info in TUnixSystem::ACLiC: creating shared library /home/TestCallFortran/./myc2f77_C.so
root [2] myc2f77(2,6)
/usr/bin/root.exe: symbol lookup error: /home/TestCallFortran/./libmyf77.so: undefined symbol: _gfortran_st_write[/quote]

I have also tried using the Intel Fortran compiler as follows

[quote]source /opt/intel/Compiler/11.0/074/bin/ifortvars.sh intel64
ifort myf77.f -shared -fPIC -c
g++ -Wl,-soname,libmyf77.so -shared -o libmyf77.so myf77.o [/quote]

I get the following [quote]
root [0] gSystem->Load(“libmyf77”)
(int)0
root [1] .L myc2f77.C++
Info in TUnixSystem::ACLiC: creating shared library /home/TestCallFortran/./myc2f77_C.so
root [2] myc2f77(2,6)
/usr/bin/root.exe: symbol lookup error: /home/TestCallFortran/./libmyf77.so: undefined symbol: for_write_seq_lis[/quote]

I have also experimented with a variety of other command line switches and the Intel C++ compiler rather than g++. In most cases these fail at the command “gSystem->Load(“libmyf77”)”.

My ROOT version details are as follows:
ROOT 5.18/00b (branches/v5-18-00-patches@22563, Apr 06 2010, 02:11:00 on linuxx8664gcc)
CINT/ROOT C/C++ Interpreter version 5.16.29, Jan 08, 2008

The Intel Fortran compiler is version l_cprof_p_11.0.074

Any guidance or help you can give on where I am going wrong or what to do next will be greatly appreciated.

Regards
myc2f77.C (172 Bytes)
myf77.txt (186 Bytes)