local installation of gcc in home directory

Viewed 2482

my question is: how do I make sure gcc automatically finds the right include files by default? what I did is compile gcc modules branch with --prefix=$HOME/.local and it worked. next thing to try it out with the next requirement: install "build2" with that new compiler. so I do

PATH="$HOME/.local/bin:$HOME/.local/libexec/gcc/x86_64-pc-linux-gnu/11.0.0/:$PATH" LD_LIBRARY_PATH="/home/p/.local/lib64" sh build2-install-0.13.0.sh --sudo false $HOME/.local/

and I get lots of errors about missing header-files, like array, stddef.h, sstream, bits/c++config.h and limits.h

adding -I$HOME/.local/{include/c++/11.0.0/x86_64-pc-linux-gnu,include/c++/11.0.0,lib/gcc/x86_64-pc-linux-gnu/11.0.0/include,lib/gcc/x86_64-pc-linux-gnu/11.0.0/include-fixed} to the g++ options fixes those, but I'd like to have those paths built into my compiler. how do I do that? where in the gcc sources do I have to change something to make gcc aware of where it had installed all those headers?

edit: the fix for my problem was simple: problem is that ~/.local/bin was symlink to ~/bin, so I only need to rm that, move ~/bin in its place and optionally create a symlink from ~/.local/bin to ~/bin. my question still stands though, maybe a bit more academic now: how do I force gcc to look for headers at a certain location by default?

1 Answers

If you use GNU Make, you have to set some of the implicit variables:

$ export CXX=/path/to/your/installation/bin/g++
$ export CC=/path/to/your/installation/bin/gcc
$ export LDFLAGS="${LDFLAGS} -L /path/to/your/installation/lib64 \
                  -Wl,-rpath -Wl,/path/to/your/installation/lib64"

where /path/to/your/installation is where you installed gcc.

Note that I export also CC which defines the C compiler. Also, export LDFLAGS="${LDFLAGS} ... prevents to overwrite previous settings of LDFLAGS.

Addition

The OP asks

how do I force gcc to look for headers at a certain location by default

gcc searches the headers of the standard library in its installation folder*. You can explicitly check this as explained here. So there is not need to add -I /path/to/your/installation to use the headers of the local installation.

Anyway, from the man pages of g++:

CPATH specifies a list of directories to be searched as if specified with -I, but after any paths given with -I options on the command line.

Exporting CPATH=... sets a default directory to search for headers.


*As noted in the comments, it appears that gcc searches its library in a path relative to the location of the executable g++/gcc, *resolving eventual symlinks*.
Related