4

My C code:

#include<stdio.h>
#include "Python.h"

int main()
{
    printf("Hello World");
    return 0;
}

I have python-dev installed for python2.7. Moreover, Python.h is available in /usr/include/python2.7.

gcc myfile.c # Python.h: No such file or directory

I even tried : gcc -L/usr/include/python2.7/ myfile.c # Python.h: No such file or directory

I tried building a python c module ujson with pip that uses Python.h, it was able to compile.

What am I missing / doing wrong ?

Yugal Jindle
  • 44,057
  • 43
  • 129
  • 197
  • 2
    use -I in place of -L, better solution: write a makefile (yeah it will require some study, but the effort is worth while). Compiling on the command line can be a big pain – BigMike Jun 08 '12 at 08:50
  • 1
    Even better, if this code is a Python extension: write a `setup.py` file and use distutils. – Fred Foo Jun 08 '12 at 09:40

2 Answers2

10

It should be -I, not -L:

gcc -I/usr/include/python2.7 myfile.c
mata
  • 67,110
  • 10
  • 163
  • 162
0

Use

#include <Python.h>

instead of

#include "Python.h"

to include the header file. The Python.h file should be the first file which is included.

@see Extending Python with C or C++ (Section 1.1 Note)

Since Python may define some pre-processor definitions which affect the standard headers on some systems, you must include Python.h before any standard headers are included.

Community
  • 1
  • 1
Nicola Coretti
  • 2,655
  • 2
  • 20
  • 22