365

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.

1

4 Answers 4

501

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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"? 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?
@PeterMortensen CPATH in the GCC/CPP docs. Edit suggested, awaiting approval.
65

Create an alias for gcc with your favorite includes:

alias mygcc='gcc -I/whatever/'

7 Comments

I think there should be no space after -I
just a matter of habit to omit the space, just like you'd type -l<libnam> 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.
"The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.", according to the GCC manual.
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.
@h4unt3r why is an alias poor?
Because, as einpoklum said, "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".
Oh, I didn't know einpoklum's was a follow up to that point. If that's the case, then sounds like a moot point. If you want more than what an alias provides, then simply don't use an alias. That doesn't make it a poor solution, just a solution to a problem other than yours.
21

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.

1 Comment

This seems to be a response to jcrossley3's answer, not an answer. Stack Overflow is not a forum. Related: Why do I need 50 reputation to comment? What can I do instead?.
11

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

See HOWTO Use the GCC specs file

3 Comments

The link is dead. Would be great if you could update this answer with the actual info.
@Thomas fortunately, it's still available on Archive.org!

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.