0

I've been exploring compilers and cross compilers. I'm reading the GCC manual. Specifically, there are these statements in the manual that I have queries regarding:

The linker searches a standard list of directories for the library. The directories searched include several standard system directories plus any that you specify with -L.

The -isystem and -idirafter options also mark the directory as a system directory, so that it gets the same special treatment that is applied to the standard system directories.

Alright, what are these "system directories"? On a Linux machine, what are the system directories for the native compiler?

And if I've built a cross compiler (like the one shown here: https://wiki.osdev.org/GCC_Cross-Compiler), what are the "system directories" with respect to this compiler?

Can I change the system directory when I build GCC? Moreover, Where does sysroot come into the picture?

Community
  • 1
  • 1
Suraaj K S
  • 600
  • 3
  • 21
  • 1
    man ld(1) [Where do executables look for shared objects at runtime?](https://unix.stackexchange.com/a/22937/293929) – 273K Apr 17 '20 at 17:22
  • The "standard" could refer to [FHS](https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard) which would mean `/lib` and `/usr/lib` etc. – KamilCuk Apr 17 '20 at 17:30

2 Answers2

1

The "standard" system directories aren't specific directories - it may vary across installations/distributions.

gcc has an option -print-search-dirs. Using which you can get the list of directories, it looks for.

Something like:

gcc -print-search-dirs | grep libraries | sed 's/libraries: =//g' | tr ':' '\n' | xargs readlink -f 

It's the same for cross compiler's too (you'd call cross compiler's front-end instead of plain gcc).

--sysroot is straightward:

--sysroot=dir Use dir as the logical root directory for headers and libraries. For example, if the compiler normally searches for headers in /usr/include and libraries in /usr/lib, it instead searches dir/usr/include and dir/usr/lib.

If you use both this option and the -isysroot option, then the --sysroot option applies to libraries, but the -isysroot option applies to header files.

The GNU linker (beginning with version 2.16) has the necessary support for this option. If your linker does not support this option, the header file aspect of --sysroot still works, but the library aspect does not.

P.P
  • 117,907
  • 20
  • 175
  • 238
  • Can one modify these system directories when building gcc? How does GCC know which system directory is for which OS? – Suraaj K S Apr 17 '20 at 17:51
  • I believe you can specify additional directories when compiling gcc. Although I don't quite know how. Perhaps you can check the latest gcc from ftp://ftp.irisa.fr/pub/mirrors/gcc.gnu.org/gcc/releases/gcc-9.3.0/ and look at configure options. – P.P Apr 17 '20 at 17:53
  • Additional directories? So what's the bare minimum then? – Suraaj K S Apr 17 '20 at 18:02
  • By additional, I meant in addition to directories needed for system libraries. When compiling gcc, default set of dirs will be set by install procedure (it's not something you need to worry about). I was under the impression that you want to *more* directories to be searched by default. – P.P Apr 17 '20 at 18:04
  • And using --sysroot would search in $(SYSROOT)/"system directories" instead of /"system directories"? Or would it search in both places? – Suraaj K S Apr 17 '20 at 18:09
  • Just the sysroot/dirs. Also note about [sysroot](https://stackoverflow.com/a/37464870/4389800). – P.P Apr 17 '20 at 18:25
  • Here's the [configure](https://github.com/gcc-mirror/gcc/blob/master/configure) script. I usually install the *vanilla* version with the defaults; you can read through for various options. – P.P Apr 17 '20 at 18:42
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/211916/discussion-between-suraaj-k-s-and-usr). – Suraaj K S Apr 18 '20 at 04:47
0

GCC is nothing if not configurable.

When you build an instance GCC, you need to completely specify the target environment. (Fortunately, most of this has already been done for you.) If necessary, you can tweak any of these settings before you do the build. The settings are described in detail in the GCC internals manual, but the particular ones you're interested in are in the chapter describing Target Macros, and particularly the section on Controlling the Compilation Driver. In this last section, you'll find descriptions of the various macros which define include paths. (Search for the word "include" in that page. Read everything you find :-); GCC documentation is not a tutorial.)

rici
  • 234,347
  • 28
  • 237
  • 341