FORTRAN and C++ embedding ROOTines

Hi,
I have a problem calling from a Fortran program C++ function with root routines.
For example the following code gives a big amount of error lines.

sample.f

      program test
      write(*,*) ' before call '
      call openfile()
      call closefile()
      write(*,*) ' after call '    
      stop
      end

sample.cxx

#include "TFile.h"
#include <stdio>
extern "C" {
TFile* gfile = 0;
void openfile_();
void closefile_();
void openfile_(){
  gfile = new TFile("sample.root","recreate");
  return;
}
void closefile_(){
  if (!gfile) { 
    gfile->Close(); 
    gfile = 0; 
  }
  return;
}
}

compilation:

g++ -Wall -c -I/amssw/root4/include sample.cxx
g77 -o sample sample.f sample.o -L/amssw/root4/lib -lRoot -lnsl -lcrypt -lssl -ldl -lfreetype 

There is some known problem between ROOT and FORTRAN or
there are a code errors?

Thanks a lot,
Alberto

Can you tell, wich errors you have? Linker’s ?

Instead of strange statements like
if (gfile) {
gfile->Close();
gfile = 0;
}

you can simple do

delete gfile;

In you command line, what does “-lRoot” mean ?

Thank you for your comment,
about error lines: ~ 3000 of undefined reference to almost all the ROOT classes methods (operator new, delete for many TClasses). These are generated by the linker (collect2: ld returned 1 exit status)

about -lRoot:
is a normal linking option for the inclusion of the static library libRoot.a, a fundamental ROOT library.

The correction proposed seems don’t solve the problem,
Alberto

[quote]Thank you for your comment,
about error lines: ~ 3000 of undefined reference to almost all the ROOT classes methods (operator new, delete for many TClasses). These are generated by the linker (collect2: ld returned 1 exit status)
[/quote]

Yes, because you specify wrong options in command line for linker.

Very interesting news. Are you sure, you have the libRoot.a file? Have you ever seen someone linking with libRoot ? I have both linux and windows versions of root, and none of them has such library. Just look at samples, which libraries you need to link. For example, -lCore, -lHist etc.
lRoot is something new :slight_smile:

Did you really try it to fix linker’s errors? :slight_smile: It was a simple remark about your C++ code, of course, it will not help to linker. :slight_smile:)

PS. I think, even after you link with ROOT’s libraries correctly (try -lCore for example instead of -lRoot), you still need to link to C++ library.

Thank you for your reply,

yes I’m sure, It’s a construction of mine with all the root shared libraries. Until this Fortran question the inclusion of this library doesn’t give any problem.

Your proposal to use the standard inclusion library (as -lCore) seems to resolve the problem, so I think that there is something wrong in the construction of my libRoot.a.

Thank you for your time,
Alberto

[quote=“alberto”]Thank you for your comment,
about error lines: ~ 3000 of undefined reference to almost all the ROOT classes methods (operator new, delete for many TClasses). These are generated by the linker (collect2: ld returned 1 exit status)
. . .
The correction proposed seems don’t solve the problem,
Alberto[/quote]

Dear Alberto,
your example:

g++ -Wall -c -I/amssw/root4/include sample.cxx g77 -o sample sample.f sample.o -L/amssw/root4/lib -lRoot -lnsl -lcrypt -lssl -ldl -lfreetype
suggests you tried to create sample.o object file from the C++ code sample.cxx and then you tried to create the object file with the same name “sample.o” from the fortran code sample.f. Can you choose the different names for your C++ and Fortran counterparts and try to build executable using "g++
rather “g77”. Something like this:

g77 -c samplef.f g++ -o sample sample.cxx samplef.o `root-config --glibs` `cernlib`
If by some reason you have no “cernlib” script availble you can use

g77 -c samplef.f g++ -o sample sample.cxx samplef.o `root-config --glibs` -lnsl -lcryptinstead.