0

I have a Ubuntu 20.04.2 LTS machine and have gcc 9.4.0 by default.

I built a GCC-12.1.0 from source and wrote a very simple cpp program and compiled it with /path_to_gcc12_bin/g++.

Then I run gdb to follow, by pressing s, and it shows the program includes the newer version c++ header files of gcc12.

The shared library libstdc++.so.6 is still linked to the system path one.

My question is :

How does /path_to_gcc12_bin/g++ knows to use the newer version(its own) header files?

I don't have CPLUS_INCLUDE_PATH env variables or anything similar set up.

If I build the program using the system g++, it would use system path include files, that is /usr/include/c++/9/bits/regex.h.

P.S. move /path_to_gcc12 the whole directory to somewhere else, g++ can still include the newer header files correctly.


test.cpp

#include <iostream>
#include <regex>
using namespace std;

int main(){
    std::regex re("Get|GetValue");
    std::cmatch m;
    cout << std::regex_search("GetValue", m, re)  << endl;  // returns true, and m[0] contains "Get"
    cout << std::regex_match ("GetValue", m, re)  << endl;  // returns true, and m[0] contains "GetValue"
    cout << std::regex_search("GetValues", m, re)  << endl; // returns true, and m[0] contains "Get"
    cout << std::regex_match ("GetValues", m, re)  << endl; // returns false
}

tian@tian-B250M-Wind:~/GCC-12.1.0/bin$ LD_PRELOAD='/home/tian/GCC-12.1.0/lib64/libstdc++.so.6.0.30' gdb ./test
(gdb) b 9
Breakpoint 1 at 0x4036f7: file test.cpp, line 9.
(gdb) r
Starting program: /home/tian/GCC-12.1.0/bin/test 

Breakpoint 1, main () at test.cpp:9
9               cout << std::regex_search("GetValue", m, re)  << endl;  // returns true, and m[0] contains "Get"
(gdb) s
std::regex_search<char, std::allocator<std::__cxx11::sub_match<char const*> >, std::__cxx11::regex_traits<char> > (__s=0x42c0f6 "GetValue", __m=..., __e=..., __f=std::regex_constants::_S_default)
    at /home/tian/GCC-12.1.0/include/c++/12.1.0/bits/regex.h:2409
2409        { return regex_search(__s, __s + _Rx_traits::length(__s), __m, __e, __f); }

at /home/tian/GCC-12.1.0/include/c++/12.1.0/bits/regex.h:2409

Rick
  • 7,007
  • 2
  • 49
  • 79
  • There is no such thing as "system path". You can have several versions of `gcc` installed from Ubuntu packages (which by the way you should have done instead of building your own). None is more or less privileged or special or "system" than the other. Each one knows where its header files are. `gcc-9` looks in `/usr/include/c++/9/` and gcc-10 looks in `/usr/include/c++/10/`. – n. m. could be an AI Jun 26 '22 at 09:46
  • It tries to guess the location of the executable in various ways, like looking at argv[0], and defines all others relative to this path (it can also use some paths defined at configure time in some circumstances). For libstdc++, note that the one used at link time and the one found at runtime are not necessarily the same. – Marc Glisse Jun 26 '22 at 09:59

1 Answers1

0

I guess gcc and g++ are finding headers in relative directory, relative to /path_to_gcc12/bin/gcc.

no matter where I mv the gcc12 directoy in my machine. It can always include its own c++ header files correctly.

Try ./g++ -g -Wall --verbose -o test test.cpp gives the include path:

tian@tian-B250M-Wind:~/Desktop/GCC-12.1.0/bin$ ./g++ -g -Wall --verbose -o test test.cpp
Using built-in specs.
COLLECT_GCC=./g++
COLLECT_LTO_WRAPPER=/home/tian/Desktop/GCC-12.1.0/bin/../libexec/gcc/x86_64-pc-linux-gnu/12.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /home/tian/playground/gcc_build_play/objdir/../gcc-12.1.0/configure --prefix=/home/tian/GCC-12.1.0 --disable-multilib
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 12.1.0 (GCC) 
COLLECT_GCC_OPTIONS='-g' '-Wall' '-v' '-o' 'test' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /home/tian/Desktop/GCC-12.1.0/bin/../libexec/gcc/x86_64-pc-linux-gnu/12.1.0/cc1plus -quiet -v -imultiarch x86_64-linux-gnu -iprefix /home/tian/Desktop/GCC-12.1.0/bin/../lib/gcc/x86_64-pc-linux-gnu/12.1.0/ -D_GNU_SOURCE test.cpp -quiet -dumpbase test.cpp -dumpbase-ext .cpp -mtune=generic -march=x86-64 -g -Wall -version -o /tmp/ccrg0qhG.s
GNU C++17 (GCC) version 12.1.0 (x86_64-pc-linux-gnu)
        compiled by GNU C version 12.1.0, GMP version 6.2.1, MPFR version 4.1.0, MPC version 1.2.1, isl version isl-0.24-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/home/tian/Desktop/GCC-12.1.0/bin/../lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../x86_64-pc-linux-gnu/include"
ignoring duplicate directory "/home/tian/Desktop/GCC-12.1.0/bin/../lib/gcc/../../lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../include/c++/12.1.0"
ignoring duplicate directory "/home/tian/Desktop/GCC-12.1.0/bin/../lib/gcc/../../lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../include/c++/12.1.0/x86_64-pc-linux-gnu"
ignoring duplicate directory "/home/tian/Desktop/GCC-12.1.0/bin/../lib/gcc/../../lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../include/c++/12.1.0/backward"
ignoring duplicate directory "/home/tian/Desktop/GCC-12.1.0/bin/../lib/gcc/../../lib/gcc/x86_64-pc-linux-gnu/12.1.0/include"
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring duplicate directory "/home/tian/Desktop/GCC-12.1.0/bin/../lib/gcc/../../lib/gcc/x86_64-pc-linux-gnu/12.1.0/include-fixed"
ignoring nonexistent directory "/home/tian/Desktop/GCC-12.1.0/bin/../lib/gcc/../../lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../x86_64-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /home/tian/Desktop/GCC-12.1.0/bin/../lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../include/c++/12.1.0
 /home/tian/Desktop/GCC-12.1.0/bin/../lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../include/c++/12.1.0/x86_64-pc-linux-gnu
 /home/tian/Desktop/GCC-12.1.0/bin/../lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../include/c++/12.1.0/backward
 /home/tian/Desktop/GCC-12.1.0/bin/../lib/gcc/x86_64-pc-linux-gnu/12.1.0/include
 /home/tian/Desktop/GCC-12.1.0/bin/../lib/gcc/x86_64-pc-linux-gnu/12.1.0/include-fixed
 /usr/local/include
 /home/tian/Desktop/GCC-12.1.0/bin/../lib/gcc/../../include
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.

More details on https://stackoverflow.com/a/72762087/5983841

Rick
  • 7,007
  • 2
  • 49
  • 79