Elad Weiss
Elad Weiss

Reputation: 4002

Should gcc find Python.h without being passed -I/usr/include/python2.7/?

I wrote a small c program on RedHat:

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

int main() {
    printf("Hello, Python!\n");
    return 0;
}

And got the following error:

main.c:1:20: fatal error: Python.h: No such file or directory

So I found the following answer: fatal error: Python.h: No such file or directory

Installed python-devel. I verify that /usr/include/python2.7/Python.h exists, and still getting the same error.

Of-course when running gcc main.c -I/usr/include/python2.7/ everything compiles fine.

My question is:

Is it correct to add -I/usr/include/python2.7/ when compiling, or is there some kind of built-in env-variable that gcc should expect (something like PYTHON_DEV_HOME).

This is kinda strange question I believe, but the reason I'm asking is because I'm getting the same error for TensorFlow (git cloned), which should be compiling off the bat. Since it isn't, I assume my environment is missing somthing...

Upvotes: 2

Views: 1275

Answers (2)

cdarke
cdarke

Reputation: 44364

That's perfectly normal.

You cannot expect gcc to read Python environment variables - gcc is independent of Python and has no connection to it. Python can use gcc, as do (probably) thousands of other products, but other compilers should be usable as well.

gcc environment variables are listed here. Take a look at CPATH which can be used instead of -I, but make sure you read exactly what it does. C_INCLUDE_PATH is an alternative.

Upvotes: 1

rkrahl
rkrahl

Reputation: 1177

You should normally specify it with -I, but - to answer the question - you can also set CPATH environment variable:

export CPATH=:/usr/include/python2.7/

and gcc will search this directory for includes without any additional switches.

Upvotes: 1

Related Questions