llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.78k stars 11.9k forks source link

Clang unable to find std headers on Mac #92260

Open spino17 opened 5 months ago

spino17 commented 5 months ago

I am new to this amazing project and C/C++ ecosystem in general. I build the llvm and clang by following the instructions on: https://clang.llvm.org/get_started.html and then tried using compiling a simple C hello-world code having #include <stdlib.h> and #include <stdio.h> but it fails with the following error: main.c:1:10: fatal error: 'stdio.h' file not found. The document doesn't specify any more setup so I don't know what I am missing. The code compiles with my mac's default clang with version: Apple clang version 14.0.3 (clang-1403.0.22.14.1) and the output of command clang --version is

Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: arm64-apple-darwin23.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

I found the headers inside the location /Library/Developer/CommandLineTools/usr/include. Does the clang or LLVM requires setting some path variable ? Also when I build llvm with providing cmake the option -DLLVM_STATIC_LINK_CXX_STDLIB:ON, during make a warning is showed clang: warning: argument unused during compilation: '-static-libstdc++' [-Wunused-command-line-argument]

llvmbot commented 5 months ago

@llvm/issue-subscribers-clang-driver

Author: Bhavya Bhatt (spino17)

I am new to this amazing project and C/C++ ecosystem in general. I build the llvm and clang by following the instructions on: `https://clang.llvm.org/get_started.html` and then tried using compiling a simple C hello-world code having `#include <stdlib.h>` and `#include <stdio.h>` but it fails with the following error: `main.c:1:10: fatal error: 'stdio.h' file not found`. The document doesn't specify any more setup so I don't know what I am missing. The code compiles with my mac's default clang with version: `Apple clang version 14.0.3 (clang-1403.0.22.14.1)` and the output of command `clang --version` is ``` Apple clang version 14.0.3 (clang-1403.0.22.14.1) Target: arm64-apple-darwin23.4.0 Thread model: posix InstalledDir: /Library/Developer/CommandLineTools/usr/bin ``` I found the headers inside the location `/Library/Developer/CommandLineTools/usr/include`. Does the clang or LLVM requires setting some path variable ? Also when I build llvm with providing cmake the option `-DLLVM_STATIC_LINK_CXX_STDLIB:ON`, during `make` a warning is showed `clang: warning: argument unused during compilation: '-static-libstdc++' [-Wunused-command-line-argument]`
vadorovsky commented 1 month ago

I'm hitting the exactly same issue with the binaries attached to the latest release: https://github.com/llvm/llvm-project/releases/tag/llvmorg-19.1.0

To reproduce that, I'm downloading and unpacking the release tarball and adding it to PATH:

$ wget https://github.com/llvm/llvm-project/releases/download/llvmorg-19.1.0/LLVM-19.1.0-macOS-ARM64.tar.xz
$ tar -xpf LLVM-19.1.0-macOS-ARM64.tar.xz
$ export PATH="/Users/vadorovsky/Downloads/LLVM-19.1.0-macOS-ARM64/bin:$PATH"
$ which clang
/Users/vadorovsky/Downloads/LLVM-19.1.0-macOS-ARM64/bin/clang

Then I'm trying to use it to compile a "Hello world" program using stdio.h:

$ cat helloworld.c
#include <stdio.h>

int main() {
  puts("hello world");
  return 0;
$ clang helloworld.c
helloworld.c:1:10: fatal error: 'stdio.h' file not found
    1 | #include <stdio.h>
      |          ^~~~~~~~~
1 error generated.

My first thought was that I just need to specify the XCode sysroot explicitly and then it will be able to find the header. So I tried, but then Apple ld started complaining:

$ clang -isysroot /Library/Developer/CommandLineTools/SDKs/
MacOSX.sdk helloworld.c
dyld[28020]: Symbol not found: ___cxa_pure_virtual
  Referenced from: <DB5BC195-AEDB-32BB-BABB-5255CEB34D87> /Library/Developer/CommandLineTools/usr/bin/ld
  Expected in:     <4C4C4451-5555-3144-A1C9-14D84BE6CB58> /Users/vadorovsky/Downloads/LLVM-19.1.0-macOS-ARM64/lib/libc++.1.0.dylib
clang: error: unable to execute command: Abort trap: 6
clang: error: linker command failed due to signal (use -v to see invocation)

Then I tried forcing the usage of lld and that worked:

$ clang -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -fuse-ld=lld helloworld.c
$ ./a.out
hello world

But I don't think that solves the problem entirely. Having to pass these flags always is a bit annoying and I also couldn't find any way to gracefully include then in CMake compiler checks in different projects apart from defining -DCMAKE_C_FLAGS, which is ugly.

I think that in order for the binary tarballs for macOS to be usable without tricks, we would need to: