Load C libraries from Python code

Hi !

The question has probably already been asked, but I really couldn’t find any satisfactory answer so here I go.

I have some C file, category.c, which defines a certain number of functions (among which CategorySimple(int)) that I would like to access from python. What I found suggested to use :

import ROOT
ROOT.gROOT.LoadMacro(“Category.cc+”)

This step works, but I do not understand how I can access categorySimple after that as it does not become an attribute of ROOT / gROOT nor a function in my program’s namespace.

What is the exact effect of LoadMacro?
Thank you in advance !

Thank you in advance for your help

This should work:

>>> import ROOT
>>> ROOT.gROOT.LoadMacro("Category.cc+")
>>> ROOT.CategorySimple()

At least it works for me:

>>> import ROOT
>>> ROOT.gROOT.LoadMacro("testgui.cxx+")
0
>>> ROOT.MyMainFrame(0, 600, 400)
<ROOT.MyMainFrame object ("fMainFrame2") at 0x06319930>
>>>

That is quite surprising, when I do this I inevitably get an “attribute error” at the last step…
The functions in the c file are defined through :
extern “C” int categorySimple(int n){…}

Could this somehow interfere with what I am trying to do?
(the LoadMacro yields 0 though)

No, using extern "C" works for me as well…
BTW, which version of ROOT are you using (and which platform/compiler)?

I’m running root 6.06.1 built for linuxx8664gcc on a linux machine
Also I use python 2.7.10

So please remove the extern "C" statement and try again…

Ok I tried a minimal exemple with just a Hello world function in a file, and clearly LoadMacro works.
Somehow however it does not seem to like the functions from Category.C

After more tests, it turns out that the problem clearly comes from the extern “C” in the definition.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.