Check CC Include Search Path

2025-03-02, Sun

So, what is the default include path of CC? i.e. for GCC in GNU/Linux, and Clang in macOS?

1. For GCC

GCC default include and library search paths could be printed out through the verbose option1 like this:

echo "" | gcc -xc -E -v /dev/fd/0

The output from my Debian GNU/Linux looks like this, with extra info ommitted for brevity:

gcc version 12.2.0 (Debian 12.2.0-14) 
COLLECT_GCC_OPTIONS='-E' '-v' '-mlittle-endian' '-mabi=lp64'
 /usr/lib/gcc/aarch64-linux-gnu/12/cc1 -E -quiet -v -imultiarch aarch64-linux-gnu /dev/fd/0 -mlittle-endian -mabi=lp64 -fasynchronous-unwind-tables -dumpbase 0
...
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/aarch64-linux-gnu/12/include
 /usr/local/include
 /usr/include/aarch64-linux-gnu
 /usr/include
End of search list.
...
COMPILER_PATH=/usr/lib/gcc/aarch64-linux-gnu/12/:/usr/lib/gcc/aarch64-linux-gnu/12/:/usr/lib/gcc/aarch64-linux-gnu/:/usr/lib/gcc/aarch64-linux-gnu/12/:/usr/lib/gcc/aarch64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/aarch64-linux-gnu/12/:/usr/lib/gcc/aarch64-linux-gnu/12/../../../aarch64-linux-gnu/:/usr/lib/gcc/aarch64-linux-gnu/12/../../../../lib/:/lib/aarch64-linux-gnu/:/lib/../lib/:/usr/lib/aarch64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/aarch64-linux-gnu/12/../../../:/lib/:/usr/lib/

Get rid of the -E flag to see library search paths, which are not enlisted here because they're too long. Refer to GNU documentation2, 3 for more info.

2. for macOS

Run the command above without -E and get below output:

...
clang -cc1 version 16.0.0 (clang-1600.0.26.3) default target arm64-apple-darwin24.3.0
...
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/16/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)
End of search list.
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -lto_library /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib -no_deduplicate -dynamic -arch arm64 -platform_version macos 15.0.0 15.0 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -mllvm -enable-linkonceodr-outlining -o a.out -L/usr/local/lib /var/folders/ks/mgh84pt14zx9j6t568j87x6h0000gn/T/0-0b39fd.o -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/16/lib/darwin/libclang_rt.osx.a
...

This output raises two questions:

  • Is there an easier way to denote the Xcode root path?
  • What's the necessary options for ld?

2.0.1. xcrun and other Xcode command tools

Turns out there are a bunch of Xcode CLI tools installed already. They could be checked through ls /usr/bin | grep xc:

xcdebug
xcode-select
xcodebuild
xcrun
xcscontrol
xcsdiagnose
xctrace

To check the SDK path for macOS, run xcrun --sdk macosx --show-sdk-path, with output like this:

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk

It's funny that the SDK name is still macosx instead of macos.

2.1. linker options

Based on the "Hello World" Assembly example4 from @Joe_A_Burnett, the required options for a basic program would be -lSystem and -syslibroot path-to-macosx-sdk, hence the following config:

-lSystem
-syslibroot `xcrun --sdk macosx --show-sdk-path`

The end.

Footnotes: