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
4
  • 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? Commented Dec 30, 2013 at 16:04
  • Sorry, i use gcc compiler (i think), OS linux mint 15(olivia) Commented Dec 30, 2013 at 16:08
  • I believe you will find your answer at this earlier question Commented Dec 30, 2013 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. Commented Dec 30, 2013 at 16:17

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.