Accessing FORTRAN subroutines within ROOT

Greetings all,

I originally posted this at the end of a 6 year old thread, but that seems not to have worked so I am trying again.

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. http://root.cern.ch/phpBB3//viewtopic.php?f=4&t=814

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

My operating system version is UBUNTU-10.04 linux-generic2.6.32.22.23. GCC version is 4:4.4.3-1ubuntu1

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)

Greetings all

Just an update: - Rene’s example runs exactly as written on a 32bit system with G77 using switch -lg2c.

So the question is simply is there a way of getting this to work on a 64bit system in which G77 has been deprecated?

Regards

I suggest to look at the various makefiles at ftp://root.cern.ch/root/pythia6.tar.gz

Rene

Thank you Rene - I now see what I was doing wrong.

Regards

Hi Russell,

[quote=“Russell Leslie”]Thank you Rene - I now see what I was doing wrong.
[/quote]

Since you posted the code that doesn’t work, can you post the code/change that does (for the googlers among us)? :slight_smile:

Thanks,
Charles

Charles,

Using gfortran on a 64-bit linux it compiled as follows

[quote] gfortran -c -m64 -fPIC myf77.f
gfortran -m64 -shared -Wl,-soname,libmyf77.so -o libmyf77.so myf77.o
[/quote]
My mistake was to leave out the -m64 and to do the linking with g++ or gcc when gfortran works better.

It also compiles using Intel Fortran using the same steps (with the ?? marks replaced with the actual path to the compiler).

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

If the Intel compiler is used then the path to the Intel libraries has to be included in the LD_LIBRARY_PATH in order to avoid LD errors when loading the shared object file in ROOT.

Regards

I was receiving the same error messages you were describing, but when I try to implement your solution (in your last post), I receive this error message after issuing the command in root:

root [0] gSystem->Load(“libmyf77”)
dlopen error: /cluster/tufts/physicshe/shamil01/TopRootWork/trialfortran/./libmyf77.so: wrong ELF class: ELFCLASS64
Load Error: Failed to load Dynamic link library /cluster/tufts/physicshe/shamil01/TopRootWork/trialfortran/./libmyf77.so
(int)(-1)

Is anyone familiar with fixing this problem? I am running on a 64-bit software, so I don’t understand this error?

The error messages seem to imply that your root install is 32-bit not 64-bit.

If that is so, I don’t understand why the original prescription for linking the fortran libraries also fails.

VIVE L’AMOUR!
you should use gfortran / ifort to link your shared library instead of g++ (i.e. use the same fortran driver to link your shared library, which you use to compile your source code).
If you don’t, you may get unresolved symbols at run-time (e.g. “_gfortran_st_write” and “for_write_seq_lis” in the original mail).
The “-lg2c” is not any compiler switch. It’s the g77 run-time library “libg2c.so” (or “.a”) added to linking.
The gfortran / ifort have their own run-time libraries (and startup codes), which are not named “libg2c.*”, of course.
Just to make it clear. If you want to use gcc / g++ to link your shared library, you should manually add appropriate, your fortran compiler related, libraries and startup object files to the linking step.
I am stupid. No?
Pepe Le Pew.