2

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...

das-g
  • 9,718
  • 4
  • 38
  • 80
Elad Weiss
  • 3,662
  • 3
  • 22
  • 50
  • 1
    https://stackoverflow.com/a/21530866/674064 seems to indicate that this is normal. – das-g Sep 05 '17 at 06:35
  • 1
    Don't set any environment variabes, pass `-I/usr/include/python2.7` to gcc explicitly. Write a makefile to automate the task. If you are creating real software that other people can build on their machines, you need some kind of auto-configuration tool that builds a makefile or a similar build script automatically according to local environment. If you are just learning you can also just use `#include instead of all that. – n. m. could be an AI Sep 05 '17 at 06:37

2 Answers2

1

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.

rkrahl
  • 1,159
  • 12
  • 18
1

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.

cdarke
  • 42,728
  • 8
  • 80
  • 84
  • The argument is actually the other way around - GCC can use thousands of libraries, some of which didn't even exist when GCC itself was built. How is GCC going to know about `/usr/include/RattleSnake1.0` ? – MSalters Sep 05 '17 at 13:50