On recent versions of macOS, the headers are hidden out of sight rather than being placed in /usr/include
as on any civilized system. You may need to set:
export CPATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
or:
PartA="/Applications/Xcode.app/Contents/Developer/Platforms/"
PartB="MacOSX.platform/Developer/SDKs/MacOSX.sdk"
export CPATH="$PartA/$PartB/usr/include"
to get the compiler to find the headers — I do that with home-built (now moderately ancient) versions of GCC on my Macs running new versions of macOS. About the only saving grace is that there are no spaces in that directory name.
Setting CPATH
may be sufficient. If it isn't, and if the directory it is set to is present, then adding:
… -I "$CPATH" …
to the compiler command line may resolve the problem. I've not needed to do that.
I have this script:
#!/bin/sh
#
# @(#)$Id: gcc-env.sh,v 1.2 2021/11/24 03:02:03 jleffler Exp $
#
# Set GCC environment for macOS Catalina 10.15 (and maybe beyond)
MACOSX_SDK="/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk"
export LIBRARY_PATH="$MACOSX_SDK/usr/lib"
export CPATH="$MACOSX_SDK/usr/include"
export MANPATH="$(clnpath $MANPATH:$MACOSX_SDK/usr/share/man)"
I use . gcc-env
to set the environment. My home-built GCC compilers then work correctly.
With that said, the out-of-the-box Xcode installation of /usr/bin/gcc
and /usr/bin/clang
etc works 'automatically'. However, after installing Xcode, you need to run the GUI (at least briefly). It should offer to install some extra stuff — have it do that extra installation.
When I run the command with the -v
option for a particular command of mine, I get the output:
$ clang -v -I/Users/jonathanleffler/inc -g -O3 -std=c11 -pedantic -Wall \
> -Wextra -Werror -Wshadow -Wmissing-prototypes -Wpointer-arith -Wold-style-definition \
> -Wcast-qual -Wstrict-prototypes -DHAVE_MEMMEM -DHAVE_STRNDUP -DHAVE_STRNLEN \
> -DHAVE_GETDELIM -o fl fl.c -L/Users/jonathanleffler/lib/64 -ljl
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.15.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -disable-free -disable-llvm-verifier -discard-value-names -main-file-name fl.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -fno-strict-return -masm-verbose -munwind-tables -target-sdk-version=10.15 -target-cpu penryn -dwarf-column-info -debug-info-kind=standalone -dwarf-version=4 -debugger-tuning=lldb -ggnu-pubnames -target-linker-version 530 -v -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -I /Users/jonathanleffler/inc -D HAVE_MEMMEM -D HAVE_STRNDUP -D HAVE_STRNLEN -D HAVE_GETDELIM -I/usr/local/include -O3 -Wall -Wextra -Werror -Wshadow -Wmissing-prototypes -Wpointer-arith -Wold-style-definition -Wcast-qual -Wstrict-prototypes -Wno-framework-include-private-from-public -Wno-atimport-in-framework-header -Wno-extra-semi-stmt -Wno-quoted-include-in-framework-header -pedantic -std=c11 -fdebug-compilation-dir /Users/jonathanleffler/src/cmd -ferror-limit 19 -fmessage-length 100 -stack-protector 1 -fstack-check -mdarwin-stkchk-strong-link -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fobjc-runtime=macosx-10.15.0 -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -vectorize-loops -vectorize-slp -o /var/folders/vn/wv38v30s4hz3yvbkdtbn049c0000gn/T/fl-e87f7e.o -x c fl.c
clang -cc1 version 11.0.0 (clang-1100.0.33.17) default target x86_64-apple-darwin19.6.0
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
/Users/jonathanleffler/inc
/usr/local/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.0/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/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 -dynamic -arch x86_64 -macosx_version_min 10.15.0 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -o fl -L/Users/jonathanleffler/lib/64 /var/folders/vn/wv38v30s4hz3yvbkdtbn049c0000gn/T/fl-e87f7e.o -ljl -L/usr/local/lib -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.0/lib/darwin/libclang_rt.osx.a
$
See also: