gcc - bunnyamin/bunnix GitHub Wiki

Compile

Configure

  • Environment variables that can be passed directly to configure.
  • Variables that can be passed directly to configure or exported as environment variables.

Path to AS and LD can be defined using

  • --with-as="/path/to/$TARGET-as"
  • --with-ld="/path/to/$TARGET-ld" and should also define
  • --with-gnu-as
  • --with-gnu-ld respectively.

Common installation paths

  • --srcdir defines the path to source directory.
  • --prefix defines path for architecture-independent files. Default /usr/local.
  • --exec-prefix defines path for architecture-dependent files. Default $PREFIX.
  • --bindir defines path for user binaries. Default $EPREFIX/bin.
  • --libdir defines path for object code libraries. Default $EPREFIX/lib.
  • --libexecdir defines path to program specific executables. Default $EPREFIX/libexec.
  • --includedir defines path for C header files. Default $PREFIX/include.

Cross compiler

  • --with-build-sysroot when build system ($MACHTYPE) is building the system, which directory to use to build GCC.
  • --with-build-time-tools defines path to target tools during the build.
  • --enable-version-specific-runtime-libs "Specify that runtime libraries should be installed in the compiler specific subdirectory (libdir/gcc) rather than the usual places. In addition, [libstdc++] include files will be installed into libdir unless you overruled it by using --with-gxx-include-dir=dirname. Using this option is particularly useful if you intend to use several versions of GCC in parallel. The default is yes for libada, and no for the remaining libraries."
  • --with-native-system-header-dir "Specifies that dirname is the directory that contains native system header files, rather than /usr/include. This option is most useful if you are creating a compiler that should be isolated from the system as much as possible. It is most commonly used with the --with-sysroot option and will cause GCC to search dirname inside the system root specified by that option."
    • --with-sysroot=$SYSROOT Specifies the sysroot directory. For example /home/user/sysroot.
    • --with-native-system-header-dir=/usr/include Tells GCC where to find system headers in the sysroot.
    • The path have to be absolute but its base is the sysroot. For example, --with-native-system-header-dir=/different_include becomes /home/user/sysroot/different_include
    • --with-local-prefix Do not specify /usr as the --with-local-prefix!

Make

Search paths

Parameter Comment
-B Defines path for the compiler to binaries, library, include, and data files.
-I Defines path for the linker to a directory containing headers.
-L Defines path for the linker to a directory containing libraries.
-l Defines path for the linker to a specific library file. For example libc.so is linked as -lc.
-Wl,option Defines option for the linker.
-iquote ‘$SYSROOT/quoted/' Note: One hyphen, no equal symbol, single quotes. The -iquote only affects header files that are included using the #include directive. For example, it is observed in SEARCH_DIR("...") when ld.bfd --verbose.
-iquote "=/quoted/" Note: One hyphen, no equal symbol, single or double quotes. Shorthand for defining ‘$SYSROOT/quoted/'.

Note: LD -I defines --dynamic-linker PROGRAM not an include directory.

Environment variables

Search paths can be defined as environment variables.

Environment variable Description
LD_LIBRARY_PATH Defines path to shared libraries for the compiled program, after its compilation. Any static library should be included in the compiled binary.
LIBRARY_PATH Defines path to shared and static libraries for GCC before compilation.
PATH Defines path to binaries.

Variables

GCC accepts some "implicit variables".

Variable Description
AR Defines host path to archive binary. Default is (Binutils) ar.
AS Defines host path to assembler binary. Default is (Binutils) as.
LD Defines host path to linker binary. Default is (Binutils) ld
CC Defines host path to C compiler binary. Default is (GCC) cc.
CCX Defines host path to C++ compiler binary. Default is (GCC) g++.
CPP Defines host path to C preprocessor binary. Default is (GCC) $(CC) -E.
OBJCOPY Defines host path to objcopy. Default is (Binutils) objcopy.
OBJDUMP Defines host path to objdump. Default is (Binutils) objdump.
RANLIB Defines host path to ranlib. Default is (Binutils) ranlib.
STRIP Defines host path to strip. Default is (Binutils) strip.

Arguments can be passed to each respective binary with variables named after them and a "FLAGS" suffix. For example:

  • ARFLAGS
  • ASFLAGS
  • LDFLAGS
  • CFLAGS
  • CXXFLAGS
  • CPPFLAGS

Additionally each variable can be specified for the build or target compilation by appending _FOR_BUILD and _FOR_TARGET respectively. For example

  • CC_FOR_BUILD
  • CFLAGS_FOR_BUILD
  • CFLAGS_FOR_TARGET Although, no official documentation has been found for _FOR_BUILD.

The library path to the linker can also be defined:

  • LDLIBS Library flags or names given to compilers when they are supposed to invoke the linker, ‘ld’. LOADLIBES is a deprecated (but still supported) alternative to LDLIBS. Non-library linker flags, such as -L, should go in the LDFLAGS variable.

System root

The system root defines a path that is prepended to header and library paths. For example:

  • Defining the system root as /path/to/sysroot/ ensures that the paths to header and library expand from it, that is if they are defined as /usr/include/ and /usr/lib/ respectively then their search paths become
    • /path/to/sysroot/usr/include/ and /path/to/sysroot/usr/lib/
Parameter Comment
--sysroot="" Two hyphens
-isysroot "" One hyphen and no equal symbol

If only -isysroot is defined then it is prepended only to the header path. If both -isysroot and --sysroot are defined then they are prepended to the header and library path respectively.

Debugging, errors, troubleshooting

C compiler cannot create executables

Compilation fails because of error:

checking for C compiler default output file name...
configure: error: in `/path/to/gnu/gcc/8.4.0/compile-aarch64':
configure: error: C compiler cannot create executables
See `config.log' for more details.

The cause is either that the host or build system x86-64 binaries are used instead of the target system aarch64 binaries, or the other way around, for

  • configure and, or
  • make.

The remedy is to define the path to the correct binaries. There are several available methods:

LD cannot find modules in path

Compilation fails because of error:

...
/path/to/sysroot/bin/aarch64-linux-gnu-ld: cannot find /path/to/sysroot/lib/libc.so.6 inside /path/to/sysroot/
/path/to/sysroot/bin/aarch64-linux-gnu-ld: cannot find /path/to/sysroot/lib/libc_nonshared.a inside /path/to/sysroot/
/path/to/sysroot/bin/aarch64-linux-gnu-ld: cannot find /path/to/sysroot/lib/ld-linux-aarch64.so.1 inside /path/to/sysroot/
collect2: error: ld returned 1 exit status
make[2]: *** [Makefile:985: libgcc_s.so] Error 1
...

The cause is because the path is undefined or mangled in lib.so:

/* GNU ld script
   Use the shared library, but some functions are only in
   the static library, so try that secondarily.  */
OUTPUT_FORMAT(elf64-littleaarch64)
GROUP ( /path/to/sysroot/lib/libc.so.6 /path/to/sysroot/lib/libc_nonshared.a  AS_NEEDED ( /path/to/sysroot/lib/ld-linux-aarch64.so.1 ) )

Should be:

/* GNU ld script
   Use the shared library, but some functions are only in
   the static library, so try that secondarily.  */
OUTPUT_FORMAT(elf64-littleaarch64)
GROUP ( /lib/libc.so.6 /lib/libc_nonshared.a  AS_NEEDED ( /lib/ld-linux-aarch64.so.1 ) )

The remedy is to configure glibc with --prefix=/ and then install with DESTDIR="/path/to/sysroot" make install.