Re: Root macro call Fortran subroutines

From: Christian Holm Christensen <>
Date: Wed, 30 Aug 2006 17:05:56 +0200

hi Weijun,

On Wed, 2006-08-30 at 08:24 -0500, Weijun Guo wrote:
> Greetings,
> I tried to follow a discussion between Rene Brun and smith277 on a
> thread:
> fortran+code
> On my PC (windows xp), I have ROOT installed with the Windows Installer
> Packages, Cygwin packages (g++, f77, etc.), Compaq Visual Fortran
> (6.5), and Visual Studio .Net 2002(7.0).
> In that thread, Rene Brun described the following procedure:
> ==============================================================
> 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

A bit of formatting and comments would be nice :)

> Create a small shared lib
> g77 -c myf77.f
> g++ -Wl,-soname, -shared -o myf77.o -lg2c

Won't work when use with MSVC compiled ROOT. This will only work with GCC compiled ROOT (different calling conventions, remember?)

> a simple C++ file myc2f77.C calling this f77 function
> #define myf77 myf77_

This may not be correct for MSVC and CF. BTW, MSVC 7 and CF 6 will probably not work. MSVC 7+ and Intel Fortran will probably.

> 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+

This calls the compiler used to compile ROOT. So if ROOT was compiled with GCC, then libmyf77 should be compiled with GCC. If ROOT was compiled with MSVC, then libmyf77 must be compiled with MSVC+DF/IF.

> root > myc2f77(2,6)

> =======================================================================
> I got the libmyf77.lib compiled successfully, but when I type in:

Using GCC or MSVC?

> System->Load("libmyf77")
> ROOT was complaining about not finding the library, which does exist in
> the working directory. The error is as following:
> Error in <TWinNTSystem::DynamicPathName>: libmyf77 does not exist in
> .;C:\root\/bin;C:\NuTCROOT\bin; C:\NuTCROOT\bin\X11;C:\NuTCROOT\mksnt;
> C:\root\bin; C:\VSNET02\Common7\IDE; (...and a lot of other directories,
> I took them out to shorten the message...) , or has wrong file extension
> (.dll)

Looks like you're using GCC to compile libmyf77 and the ROOT you use is compiled with MSVC. You have 2 options:

  1. Use the ROOT compiled with GCC, and compile and link libmyf77 with GCC and G77. The above commands should work exactly as written.
  2. Use the ROOT compiled with MSVC, and compile and link libmyf77 with MSVC and DF/IF. you need to modify the commands to compile the libmyf77 library to make a DLL out of the myf77.obj
    • note, that MSVC should be used to link the DLL. Also make sure that you export all needed symbols.

Option 1, is in my mind, the easiest, but option 2 is more windozish. Your choice. A third option would be to install a proper OS ;-)

> I guess this is due to some possible incompatible factors between my
> windows version of ROOT and g77 compiler.

Never, ever mix GCC and MSVC code _unless_ you take extreme precaution, and only use C. Even in that case, there's no guarantee that it will work.

> ------------------------------------------------------------------
> This e-mail, including any attached files, may contain confidential
> and privileged information for the sole use of the intended recipient.
> Any review, use, distribution, or disclosure by others is strictly
> prohibited. If you are not the intended recipient (or authorized to
> receive information for the intended recipient), please contact the
> sender by reply e-mail and delete all copies of this message.

For your eyes only, eh! :) Too bad that's some 1000's of pairs of eyes. Perhaps this mail cannot be posted on the roottalk archive? (who's the `...intended recipient?')


 ___  |  Christian Holm Christensen 
  |_| |  -------------------------------------------------------------
    | |  Address: Sankt Hansgade 23, 1. th.  Phone:  (+45) 35 35 96 91
     _|           DK-2200 Copenhagen N       Cell:   (+45) 24 61 85 91
    _|            Denmark                    Office: (+45) 353  25 404
 ____|   Email:               Web:
 | |
Received on Wed Aug 30 2006 - 17:06:50 MEST

This archive was generated by hypermail 2.2.0 : Mon Jan 01 2007 - 16:32:00 MET