334

I'd like gcc to include files from $HOME/include in addition to the usual include directories, but there doesn't seem to be an analogue to $LD_LIBRARY_PATH.

I know I can just add the include directory at command line when compiling (or in the makefile), but I'd really like a universal approach here, as in the library case.

Lii
  • 10,777
  • 7
  • 58
  • 79
Jesse Beder
  • 31,716
  • 20
  • 105
  • 143
  • 4
    Here is link to GCC 4.8.1 manual where [C_INCLUDE_PATH and CPLUS_INCLUDE_PATH](http://gcc.gnu.org/onlinedocs/gcc-4.8.1/gcc/Environment-Variables.html#Environment-Variables) environment variables are documented. – mloskot Jan 23 '10 at 16:59

4 Answers4

454

Try setting C_INCLUDE_PATH (for C header files) or CPLUS_INCLUDE_PATH (for C++ header files).

As Ciro mentioned, CPATH will set the path for both C and C++ (and any other language).

More details in GCC's documentation.

rtx13
  • 2,532
  • 1
  • 5
  • 22
jcrossley3
  • 11,276
  • 4
  • 30
  • 32
56

Create an alias for gcc with your favorite includes.

alias mygcc='gcc -I /whatever/'
bstpierre
  • 28,439
  • 14
  • 66
  • 101
dirkgently
  • 104,737
  • 16
  • 128
  • 186
  • 18
    I think there should be no space after `-I` – Iulius Curt Jul 03 '12 at 09:58
  • 12
    just a matter of habit to omit the space, just like you'd type `-l` to link Just note that creating an alias is a very poor solution, really you would build a list of your 'favorite includes' and add them in your makefile. – h4unt3r May 17 '13 at 17:56
  • 9
    _"The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended."_, according to the GCC manual. – Jori Apr 15 '14 at 09:34
  • 4
    Not a good idea. This is non-composable (what if you want another dir? what if you want some other GCC switch) and may confuse various scripts or automated tools which make assumptions about gcc. – einpoklum Feb 23 '20 at 12:54
12

just a note: CPLUS_INCLUDE_PATH and C_INCLUDE_PATH are not the equivalent of LD_LIBRARY_PATH. LD_LIBRARY_PATH serves the ld (the dynamic linker at runtime) whereas the equivalent of the former two that serves your C/C++ compiler with the location of libraries is LIBRARY_PATH.

Lakhwinder Singh
  • 5,430
  • 5
  • 25
  • 50
Dagim Sisay
  • 121
  • 1
  • 2
10

A gcc spec file can do the job, however all users on the machine will be affected.

See HOWTO Use the GCC specs file

0xC0000022L
  • 19,426
  • 9
  • 77
  • 140
dimba
  • 25,491
  • 29
  • 135
  • 190