cmacosclangheader-filesarm64

Is there a way to fix my Clang prebuilt binary not being able to find standard header files like "stdio.h"?


I downloaded a prebuilt Clang Binary for Arm64 MacOS from the official llvm.org website. I unzipped it and tried compiling some C code with it but when I tried doing that, it gave me an error.

Error: "main.c:1:10: fatal error: 'stdio.h' file not found
    1 | #include <stdio.h>
      |          ^~~~~~~~~
1 error generated."

But if I compile the same C code with my normal Clang Compiler it works. Can someone please give me a fix to this. EDIT: "I tried using the -I flag when compiling my C code as some of you suggested but I get another really long error."


Solution

  • The problem is that the macOS SDK is not shipped with macOS itself, but with Xcode and the Xcode Command Line Tools. This means that the SDK will not be in a global, canonical path, but under the installation folders of Xcode or the CLI tools, and also that you might have multiple SDKs installed side by side.

    The default SDK paths are:

    You can find all installed SDKs with:

    find /Library/Developer/CommandLineTools /Applications/Xcode*.app -name 'MacOSX*.sdk'
    

    Both AppleClang (the one shipped with Xcode) and Clang from Brew are built with an adjusted default SDK path. Upstream Clang can't do that, but you can manually point it at any installed SDK with --sysroot like so:

    clang --sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
    

    Installation

    It's evident you already have an SDK installed somewhere, but just for reference: