Sunday, March 6, 2011

Cannot include Python.h problem solved: Calling python functions from a C program in Ubuntu

Hi all. I'm sure many of you who have tried to call python functions from a C program have faced the above problem or you were trying to build/install something.

I'm writing this solution down as many have suggested to install python-dev2.5 package, etc. Installing more than 1 python package in Linux might create a confusion. So, I'm writing the solution down.

In a Ubuntu 10.4 OS, this problem can be solved by including the Python.h file in the folder /usr/include/python2.6

Don't panic if you don't find the file header i.e. Python.h in the folder mentioned above. You wanna install the python2.6-dev package. Simply go to your command prompt (err... Terminal) and execute the following command: sudo apt-get install python2.6-dev .This will install the Python.h file(and many more headers) in your /usr/include/python2.6 folder.

Then include the header by giving the command: g++ new.cpp -I /usr/include/python2.6 -pthread -lm -ldl -lutil -lpython2.6 -o new

Hope this solved your problem of including the header.

In order to embed the python function (program) in your C++ or C code, you might wanna refer this

No comments:

Post a Comment