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 ?

2
  • 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 Commented Jun 8, 2012 at 8:50
  • 1
    Even better, if this code is a Python extension: write a setup.py file and use distutils. Commented Jun 8, 2012 at 9:40

2 Answers 2

10

It should be -I, not -L:

gcc -I/usr/include/python2.7 myfile.c
Sign up to request clarification or add additional context in comments.

Comments

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.

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.