Get Sysroot that gcc or g++ Compiler Would Use for Compilation

Get Sysroot that gcc or g++ Compiler Would Use for Compilation

When cross-compiling application or working with customized compiler toolchains, it is often necessary to determine the sysroot directory used by the compiler. The sysroot defines the base location for target headers and libraries, allowing the compiler to build applications against an alternate root filesystem. Verifying the active sysroot can help diagnose include path issues, library mismatches, and toolchain configuration problems. This tutorial explains how to get the sysroot that gcc or g++ compiler would use for compilation.

Both gcc and g++ provide the -print-sysroot option, which outputs the target sysroot configured for the compiler. The reported value may originate from the compiler build configuration.

gcc -print-sysroot
g++ -print-sysroot

Output example:

/opt/x86_64-linux-gnu/x86_64-linux-gnu/sysroot

The returned path represents the root directory that the compiler uses when searching for target-specific header files and libraries. If no sysroot has been configured for the compiler, the command produces an empty result.

Leave a Comment

Cancel reply

Your email address will not be published.