5

I am well aware of the many possibilities that exist to allow C code to run python code, and vice versa (Cython, Boost.Python, ...). However, unless I am mistaken, all those approaches merely "call" the relevant python scripts and manage the interactions between the C program and the python script. Therefore, an installation of python is required.

In my situation, I would like a stand-alone solution, where my python code can be somehow compiled and linked to my main C++ program. I had hopes with Cython, as it allowed me to compile my script and create a .so file. However, I don't seem to have been able to "link" that .so file to my C++ program. I attempted the following:

A simple python script containing a function multiply(a,b) which returns a*b ; I created a libmultiply.so file using cython. A short Cpp file which outputs the result of multiply(5,2):

int multiply(int, int);

int main()
{
    std::cout << multiply(5,2) << std::endl;
}

I build by doing : g++ test.cpp -L/home/jerome/ -lmultiply

Which gives me the error :

test.cpp:(.text+0x2b): undefined reference to `multiply(int, int)'
collect2: error: ld returned 1 exit status

I am not sure if what I tried makes sense, but hopefully it gives you an idea of what I would like to achieve.

jerorx
  • 568
  • 6
  • 19
  • Cython can't do that. It outputs code which links to the Python/C API. Unless that API is available, anything you build out of Cython isn't going to work. It also (AFAIK) does some fairly aggressive name mangling. – Kevin Dec 15 '14 at 14:28
  • You can't 'compile' Python code; it works off a VM. However, you can 'freeze' it, with something like '[PyInstaller](https://pypi.python.org/pypi/PyInstaller)' (which can create DLLs/SOs). Is that what you're trying to do? Or maybe "[trying to understand linking procedure for writing python/c hybrid](http://stackoverflow.com/questions/9826311/trying-to-understand-linking-procedure-for-writing-python-c-hybrid?rq=1)" will help. – Agi Hammerthief Dec 15 '14 at 14:36
  • 1
    @snotwaffle You can compile it. You just have to write the compiler:-) (since I'm not aware of one that is readily available). – James Kanze Dec 15 '14 at 16:19
  • You do realize that when you write `def multiply(a, b): return a * b` in Python, the corresponding function in C++ is _not_ `int multiply( int a, int b);`, but something more like `boost::any multiply( boost::any a, boost::any b );`. Except, of course, that it knows nothing about `boost::any`; the actual function would have a signature like `PyObject* multiply( PyObject*, PyObject* ). And once you've got all of the support for `PyObject`, you might as well link in `python.dll`. – James Kanze Dec 15 '14 at 16:26
  • @JamesKanze Yes, you're right, of course, but that would be using a sledgehammer to hit a thumb-tack in the case of the question asked. – Agi Hammerthief Dec 17 '14 at 08:46
  • @snotwaffle Except that that's why he can't possibly find a signature for his Python function. – James Kanze Dec 17 '14 at 11:17

1 Answers1

3

Shed Skin is the closest thing I could find. It compiles a typed subset of Python to c++. Probably not as robust as you'd like but this is an odd use case. If you feel like writing up something yourself, you could look into LLVM which has been used to create things similar to what you want.

Edit 1:

I just found this list of awesome python things on github, Awesome-python, and it links to Pyston which is a python LLVM implementation. May be a better fit for what you want or a starting point for a Python to C++ bridge.

bob0the0mighty
  • 782
  • 11
  • 28