ChatGPT解决这个技术问题 Extra ChatGPT

How to add a default include path for GCC in Linux?

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.

Here is link to GCC 4.8.1 manual where C_INCLUDE_PATH and CPLUS_INCLUDE_PATH environment variables are documented.

r
rtx13

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.


b
bstpierre

Create an alias for gcc with your favorite includes.

alias mygcc='gcc -I /whatever/'

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.
L
Lakhwinder Singh

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.


0
0xC0000022L

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

See HOWTO Use the GCC specs file


The link is dead. Would be great if you could update this answer with the actual info.