views:

604

answers:

9

Hello,

I'm learning C++, because it's a very flexible language, but for internet things like Twitter, Facebook, Delicious and others, Python is so much more better, then i want to know if it's possible to integrate C++ and Python in a same project. Thanks!

+1  A: 

You can write python extensions in C++. Basically python itself is written in C and you can use that to call into your C code. You have full access to your python objects. Also check out Boost.Python.

Matt Price
Thanks for the docs!
Nathan Campos
Also check out the doc OttoA posted: http://docs.python.org/extending/extending.html
Matt Price
Python != CPython. CPython is written in C. C != C++.
wuub
Python is written in C and not C++. That's why many nice C++ features (like automatic reference counting) must be done by hand if you use the Python C-API.
ascobol
+5  A: 

Yes, it is possible, encouraged and documented. I have done it myself and found it to be very easy.

Otto Allmendinger
Thanks for the experience and the very nice documentation!
Nathan Campos
How many functions/classes dit you expose to Python to find this task easy ?
ascobol
@ascobol: you get the hang of it pretty quickly.
Matt
+2  A: 

Python/C API Reference Manual - the API used by C and C++ programmers who want to write extension modules or embed Python.

Extending and Embedding the Python Interpreter

describes how to write modules in C or C++ to extend the Python interpreter with new modules. Those modules can define new functions but also new object types and their methods. The document also describes how to embed the Python interpreter in another application, for use as an extension language. Finally, it shows how to compile and link extension modules so that they can be loaded dynamically (at run time) into the interpreter, if the underlying operating system supports this feature.

gimel
+3  A: 

Try Pyrex. Makes writing C++ extensions for Python easier.

Brian
+1  A: 

See this:

Extending Python with C or C++

"It is quite easy to add new built-in modules to Python, if you know how to program in C. Such extension modules can do two things that can't be done directly in Python: they can implement new built-in object types, and they can call C library functions and system calls.

To support extensions, the Python API (Application Programmers Interface) defines a set of functions, macros and variables that provide access to most aspects of the Python run-time system. The Python API is incorporated in a C source file by including the header "Python.h". "

http://www.python.org/doc/2.5.2/ext/intro.html

PS It's spelt "integrate" :)

Larry Watanabe
+10  A: 

Interfacing Python with C/C++ is not an easy task.

Here I copy/paste a previous answer on a previous question for the different methods to write a python extention. Featuring Boost.Python, SWIG, Pybindgen...

  • You can write an extention yourself in C or C++ with the Python C-API.

    In a word: don't do that except for learning how to do it. It's very difficult to do it correctly. You will have to increment and decrement references by hand and write a lot of code just to expose one function, with very few benefits.

  • Swig:

    pro: you can generate bindings for many scripting languages.

    cons: I don't like the way the parser works. I don't know if the made some progress but two years ago the C++ parser was quite limited. Most of the time I had to copy/past my .h headers add some % characters and give extra hints to the swig parser.

    I was also needed to deal with the Python C-API from time to time for (not so) complicated type conversions.

    I'm not using it anymore.

  • Boost.Python:

    pro: It's a very complete library. It allows you to do almost everything that is possible with the C-API, but in C++. I never had to write C-API code with this library. I also never encountered bug due to the library. Code for bindings either works like a charm or refuse compile.

    It's probably one of the best solutions currently available if you already have some C++ library to bind. But if you only have a small C function to rewrite, I would probably try with Cython.

    cons: if you don't have a pre-compiled Boost.Python library you're going to use Bjam (sort of make replacement). I really hate Bjam and its syntax.

    Python libraries created with B.P tend to become obese. It also takes a lot of time to compile them.

  • Py++: it's Boost.Python made easy. Py++ uses a C++ parser to read your code and then generates Boost.Python code automatically. You also have a great support from its author (no it's not me ;-) ).

    cons: only the problems due to Boost.Python itself.

  • Pybindgen:

    It generates the code dealing with the C-API. You can either describe functions and classes in a Python file, or let Pybindgen read your headers and generate bindings automatically (for this it uses pygccxml, a python library wrote by the author of Py++).

    cons: it's a young project, with a smaller team than Boost.Python. There are still some limitations: you cannot expose your own C++ exceptions, you cannot use multiple inheritance for your C++ classes.

    Anyway it's worth trying!

  • Pyrex and Cython:

    Here you don't write real C/C++ but a mix between Python and C. This intermediate code will generate a regular Python module.

  • A new one: On 2009/01/20, the author of Py++ announced a new package for interfacing C/C++ code with python. It is based on ctypes. I didn't try it already but I will some day.

ascobol
+1  A: 

Boost.Python. Use the best bits of both languages!

MattyT
+2  A: 

I've used PyCxx http://cxx.sourceforge.net/ in the past and i found that it was very good.

It wraps the python c API in a very elegant manner and makes it very simple to use. It is very easy to write python extension in c++. It is provided with clear examples so it is easy to get started.

I've really enjoyed using this library and I do recommend it.

luc
+3  A: 

We use swig very successfully in our product.

Basically swig takes your C++ code and generates a python wrapper around it.

Alan