2

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?

user11566470
  • 53
  • 1
  • 6

1 Answers1

4

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*.
francesco
  • 7,189
  • 7
  • 22
  • 49
  • yes, my search on stack-overflow also resulted in the advise to execute gcc and g++ with full path to the binary, so it finds its headers. sadly build2 explicitely executes `gmake -j4 -f ./bootstrap.gmake CXX=g++`, and I cannot change that easily without failing checksum test. what I want is everytime this user executes g++ it should use the one in homedirectory, without the need to give the full path. any idea where in the gcc sources I can alter how the program finds its own binary, so it doesn't need to rely on that? – user11566470 Jul 20 '20 at 20:35
  • @user11566470 uhm, in principle unix [searches the executables from ```$PATH```](https://superuser.com/questions/238987/how-does-unix-search-for-executable-files). Are you sure that $PATH is set correctly? For example, if you do ```export PATH=...(your string above)``` and then ```g++ -v```, do you execute your local installation? – francesco Jul 20 '20 at 20:50
  • thanks to you I got reminded how to fix it as I had similar problems in past: many unix programs get into trouble if the path to the executable consists of symlinks or even worse hardlinks... – user11566470 Jul 20 '20 at 21:09
  • "gcc searches the headers of the standard library in its installation folder." is wrong! `cd; rm bin;mv .local/bin/ . ;ln -sf $HOME/bin .local/ ;$HOME/.local/bin/g++ -v delme.h` proves gcc isn't making use of --prefix but instead resolves symlinks in its binary's path and suddenly believes to have been installed in $HOME. I get `ignoring nonexistent directory "/home/p/bin/../lib/gcc/x86_64-pc-linux-gnu/11.0.0/include"` for example! – user11566470 Jul 20 '20 at 22:41
  • @user11566470 I mean, if you mess up the installation, you cannot pretend it still works... – francesco Jul 21 '20 at 06:28
  • sorry if I made the impression as if I would have such an expectation. I should put it differently then: in your answer please add the info that having a symlink of non-equivalent depth instead of the directory "bin" or inside the path "lib/gcc/x86_64-pc-linux-gnu/11.0.0" will mess up the installation! or better yet maybe point out where this is warned about in gcc instructions? – user11566470 Jul 21 '20 at 20:19
  • Note, that presented solution works with `cmake` too. – kubus May 09 '22 at 01:00