1

I am using this example from python documentation

#include <Python.h>
int
main(int argc, char *argv[])
{
  Py_SetProgramName(argv[0]);  /* optional but recommended */
  Py_Initialize();
  PyRun_SimpleString("from time import time,ctime\n"
                     "print('Today is', ctime(time()))\n");
  Py_Finalize();
  return 0;
}

where python script is hard-coded to a C program. But when i try to compile it by

$ gcc -c modwithpy.c -o mod

i receive an error:

modwithpy.c:1:20: fatal error: Python.h: No such file or directory compilation terminated.

however, i have already install python-dev package. I also looked at compiling and linking documentation and don't understand what absolute path for python package i need to write.

$ whereis python
python: /usr/bin/python3.3m /usr/bin/python /usr/bin/python2.7-config 
/usr/bin/python3.3 /usr/bin/python2.7 /etc/python /etc/python3.3 /etc/python2.7
/usr/lib/python2.6 /usr/lib/python3.3 /usr/lib/python2.7 /usr/bin/X11/python3.3m
/usr/bin/X11/python /usr/bin/X11/python2.7-config /usr/bin/X11/python3.3
/usr/bin/X11/python2.7 /usr/local/lib/python3.3 /usr/local/lib/python2.7
/usr/include/python2.7 /usr/share/python /usr/share/man/man1/python.1.gz
Carlos Robles
  • 10,828
  • 3
  • 41
  • 60
Nikolas
  • 2,322
  • 9
  • 33
  • 55
  • You need to find where `Python.h` lives, then make sure to add that path to the `INCLUDE` path for your compiler. What compiler are you using? – Floris Dec 30 '13 at 16:04
  • Sorry, i use gcc compiler (i think), OS linux mint 15(olivia) – Nikolas Dec 30 '13 at 16:08
  • I believe you will find your answer at [this earlier question](http://stackoverflow.com/questions/8282231/ubuntu-i-have-python-but-gcc-cant-find-python-h) – Floris Dec 30 '13 at 16:15
  • You need to specify the directory where the `Python.h` header is found. You seem to have multiple versions around, so you need to decide which you want to link with — probably the 3.3 version. So, there is probably a `/usr/bin/python3.3-config` (or `python3.3m-config`) file; use that. There is definitely a 2.7 config, so if you're OK with linking to Python 2.7, you can use that. – Jonathan Leffler Dec 30 '13 at 16:17

1 Answers1

5

You didn't read quite far enough. The documentation here shows how to tell the compiler where python headers and libraries are located.

Based on this, try

gcc `/opt/bin/python3.3-config --cflags` modwithpy.c -o mod \
  `/opt/bin/python3.3-config --ldlags`

If your python installed scripts in a different place, you will have to change the /opt/bin to the place where ...-config is really located. From your whereis trace, it could be /usr/bin.

Gene
  • 46,253
  • 4
  • 58
  • 96