Calling ROOT (C++) command from a C code or viceversa

Hi ROOTers,
i have to use a preexisting-C program that reads data from CASTOR and put them on an ASCII or binary file. Since now i’ve passed these HUGE files (of more that 2GB) to an external ROOT program that use these files as an input for doing data reduction. However since the whole structure is not easy to handle, i would like, in some way, to unify the C and the C++ program:

  1. my first idea was to let the C program call some C++ (ROOT) routine that fills a TTree (with compression) is order to make the subsequent analysis easier. But i don’t know how (and if it’s possible) to call a C++ routine from a C program. Is there some ROOT routine that perform this?
  2. the best way would be to pass the data that the C program itself takes from CASTOR to the C++ analysis program. But here come again: how is it possible to “call” the C program from the C++ one?

In principle the 2) would be more efficient but i would be interested in knowing also how to proceed with 1) :slight_smile:

Thank you very much in advance for an eventual reply,
Regards,
Marco

You can easily mix C and C++. See, for example,
parashift.com/c+±faq-lite/m … d-cpp.html

I very often compile standalone code that links against the root libraries (you just need to tell it to link against the root shared libraries).

Good luck,
Charles

Hi,
thanks for the link. Actually i’ve modified all the include files with the

#ifdef __cplusplus
 extern "C" {
 #endif

The code seems to compile well but actually no a.out file is produced. No modification has been applied to the C code since it contains lines applicable also to C++. This is an example of the execution of the code; i’ve added the -Wall option in order to get all possible warnings.

calviani $ g++ -Wall marco_new_single.c
marco_new_single.c: In function `int main(int, char**)':
marco_new_single.c:546: cannot convert `char*' to `BANK_NAME*' for argument `1'
   to `int DetectorIs(BANK_NAME*, char*)'
marco_new_single.c:560: cannot convert `char*' to `BANK_NAME*' for argument `1'
   to `int DetectorIs(BANK_NAME*, char*)'
marco_new_single.c:637: invalid conversion from `char*' to `unsigned char*'
marco_new_single.c:58: warning: unused variable `int wr_size'
marco_new_single.c:69: warning: unused variable `int mstage'
marco_new_single.c:72: warning: unused variable `float prot_int'
marco_new_single.c:120: warning: unused variable `int jump_back'
marco_new_single.c:121: warning: unused variable `int jump_forward'
marco_new_single.c:122: warning: unused variable `int dist_to_header'
marco_new_single.c:156: warning: unused variable `int ipippo'
marco_new_single.c:157: warning: unused variable `int icou'
marco_new_single.c:161: warning: unused variable `int fid'

If i change the compiler to gcc the program compiles correctly. I’m running these code on lxplus at CERN so,

$ gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.3/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-56)

What can be the reason for this behaviour ? (sorry if this is a little bit OT with respect of this list)

Thanks,
marco

When you call ‘gcc’ you are invoking the C compiler; ‘g++’ is the c++ compiler (it is only one executable, but it knows how you call it).

Yes, i know. This is why i’ve tried to compiler the C program with C++! I thought that only in this way i can call a ROOT routine inside the C program. But actually g++ is not building the C program at all, so at this point, is not important which ROOT call i will do inside the code!

Regards,
marco

Hi Marco,

I can’t tell anything without seeing the source code (and maybe nothing even if I do :smiley: ).

Is there any reason not to compile everything as C++ (C++ is almost a superset of C)?

Cheers,
  Charles

p.s. The first two items are your error. I don’t know why this works for C, but not C++, but if it doesn’t compile under C++, it may not be doing what you want under C.

marco_new_single.c: In function `int main(int, char**)': marco_new_single.c:546: cannot convert `char*' to `BANK_NAME*' for argument `1' to `int DetectorIs(BANK_NAME*, char*)'

Hi,
actually it was due to some error in defining an unsigned char*. Now it works, thanks.

Regards,
Marco