I use the ROOT feature of exporting C++ classes into python a lot (generating cint dict + compile libs).
So far I have been only “using C++ class in python” and not the other way around (i.e. operate on PyObject in my C++ function).
Though not an expert I know how to write C/C++ API for python.
I have a problem, however, to compile my source code with:
what is the goal? Unless you want to call the Python C-API from a CINT macro or the CINT interpreter, you should put all such calls in a .cxx, not in the .h seen by rootcint.
Sorry for being unclear.
My goal is to operate or return PyObject.
For instance, as I described one particular goal as a wished example, I would like to write a simple C++ function that fills python list with integers and return (and call it from python).
But really any example to get started would be great.
My code snipset has no use case, but just to demonstrate a compiler error once I call #include “Python.h”.
I put function implementation in the header thinking that would be easier for others to read.
As described I included Python.h for using one of API function PyFloat_FromDouble.
yes. I mean, if you really insist on implementing everything in the header, then that is still possible. For example:#ifndef PYTEST_SAMPLE_H
#define PYTEST_SAMPLE_H
#include <iostream>
#ifdef __CINT__
struct _object;
typedef _object PyObject;
#else
#include "Python.h"
#endif
class sample{
public:
sample(){}
virtual ~sample(){};
PyObject* get() const
#ifdef __CINT__
;
#else
{
double res=1.0;
return PyFloat_FromDouble(res);
}
#endif
};
#endifBut it makes life needlessly complicated compared to having a .cxx to go with it.
Yep. I originally had the function implementation in the source but I did not know hiding #include was the key (actually I didn’t realize it was cint causing a problem).
Thank you very, very much. And sorry for a really boring question (apparent to me now)…