349

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jesse Beder
  • 33,081
  • 21
  • 109
  • 146
  • 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

479

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

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

On Windows these may be set to semicolon-separated lists. On most other platforms they may be set to colon-separated lists.

More details in GCC's documentation.

Will Chen
  • 482
  • 4
  • 12
jcrossley3
  • 11,576
  • 4
  • 31
  • 32
  • What is this "CPATH" line that was added 2016? It seems to be completely bogus. Is it? At best, it could refer to a comment that has now been deleted. By user "[Ciro Santilli](https://stackoverflow.com/users/895245/ciro-santilli-ourbigbook-com)"? Does "`CPATH`" actually mean "`C_INCLUDE_PATH`"? Or is it literally "`CPATH`"? Perhaps elaborate in the answer with an explanation and/or a *direct* reference to documentation? – Peter Mortensen Nov 05 '22 at 01:03
  • @PeterMortensen [`CPATH` in the GCC/CPP docs.](https://gcc.gnu.org/onlinedocs/cpp/Environment-Variables.html) Edit suggested, awaiting approval. – Will Chen Dec 13 '22 at 17:05
60

Create an alias for gcc with your favorite includes.

alias mygcc='gcc -I /whatever/'
bstpierre
  • 30,042
  • 15
  • 70
  • 103
dirkgently
  • 108,024
  • 16
  • 131
  • 187
  • 23
    I think there should be no space after `-I` – Iulius Curt Jul 03 '12 at 09:58
  • 15
    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
  • 11
    _"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
  • 5
    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
  • @h4unt3r why is an alias poor? – Kröw May 29 '23 at 01:06
16

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dagim Sisay
  • 161
  • 1
  • 2
  • This seems to be a response to [jcrossley3's answer](https://stackoverflow.com/questions/558803/how-can-i-add-a-default-include-path-for-gcc-in-linux/558819#558819), not an answer. Stack Overflow [is not a forum](https://meta.stackoverflow.com/questions/36818/would-you-recommend-stackexchange-sites-vs-other-types-of-forum/36828#36828). Related: *[Why do I need 50 reputation to comment? What can I do instead?](https://meta.stackexchange.com/questions/214173/)*. – Peter Mortensen Nov 05 '22 at 01:16
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
  • 20,597
  • 9
  • 86
  • 152
dimba
  • 26,717
  • 34
  • 141
  • 196
  • 6
    The link is dead. Would be great if you could update this answer with the actual info. – Thomas Feb 21 '21 at 16:39
  • @Thomas fortunately, it's still available on Archive.org! – Gwyneth Llewelyn Aug 06 '23 at 01:23
  • Better than nothing, but [Always quote the most relevant part of an important link, in case the external resource is unreachable or goes permanently offline.](https://stackoverflow.com/help/how-to-answer). – Thomas Aug 07 '23 at 07:00