clangd: System header not found

Hello, I am using clangd in Visual Studio code, via vscode-clangd extension. I am on MacOsX, clangd is installed using macports, (port install clang-8.0)

It seems clangd is unable to find sytem include headers. I basically run clangd with the following command line : clangd -resource-dir=/opt/local/libexec/llvm8.0/lib/clang/8.0.0 -background-index -log=verbose

This is the file I’m testing with :

#include <string>

int main(int argc, char* argv[]) {
}

This is my compile_commands.json :

[
{
 "directory": "/tmp/build",
 "command": "/opt/local/bin/clang++    -g -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk   -o CMakeFiles/test.dir/test.cpp.o -c /tmp/test.cpp",
 "file": "/tmp/test.cpp"
}
]

This is an excerpt from the output I get :

V[20:10:00.226] Preamble for file /private/tmp/test.cpp cannot be reused. Attempting to rebuild it.
V[20:10:00.228] Ignored diagnostic. /tmp/test.cpp:1:10:'string' file not found
Skipping TU due to uncompilable errors
E[20:10:00.228] Indexing /tmp/test.cpp failed: IndexingAction failed: has uncompilable errors

Any ideas what could be wrong ?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 19 (5 by maintainers)

Most upvoted comments

I have an interesting version of this problem. I’m also trying to use clangd in VS Code on macOS Catalina. I installed it today from homebrew (LLVM 10.0), which should contain the latest fixes if I understood this thread correctly. I still had problems with clangd not finding the system header files. My original compile_commands.json:

[
{
  "directory": "/Users/gian/Desktop/test/build",
  "command": "/usr/bin/clang++    -g -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk   -o CMakeFiles/main.dir/main.cpp.o -c /Users/gian/Desktop/test/main.cpp",
  "file": "/Users/gian/Desktop/test/main.cpp"
}
]

Notice that /usr/bin/clang++ is used as compiler because I selected the corresponding “kit” in the CMake toolbar. I tried various things to fix this but no combination of new flags for clangd seemed to help.

What finally fixed the problem was changing the kit to [Unspecified], which changes the compile_commands.json to

[
{
  "directory": "/Users/gian/Desktop/test/build",
  "command": "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++    -g -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk   -o CMakeFiles/main.dir/main.cpp.o -c /Users/gian/Desktop/test/main.cpp",
  "file": "/Users/gian/Desktop/test/main.cpp"
}
]

When CMake itself is selecting the compiler, it chooses /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ instead of /usr/bin/clang++. I don’t know what the difference between those two executable is (apart from location). They return exactly the same version:

Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin19.4.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

(they are not symlinked)

My problem is resolved but I thought, I post a comment in case someone else runs into a similar issue.

Had the same issue Could you test your installation with simple compile_flags.txt file (temporary moving compile_commands.json somewhere else before tests)?

cc -E -x c++ - -v < /dev/null 2>&1 | \
    awk '/End of search list./ { show=0 } \
    { if (show) printf "-I%s\n",$1 }; \
    /#include <...> search starts here:/ { show=1; }' \
    > compile_flags.txt

The open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg solution is not a long-term one. Apple has deprecated /usr/include. If you upgrade to Catalina, you will no longer have this option.

@nicolasmartin3d wrote:

clang provided with MacOsX does not provide this tool (clangd).

I came across this issue from searching, and want to point out that Xcode does include clangd, it just isn’t on the default PATH. It can be run using xcrun -r clangd.

@GianUlli thanks! As I understand it, /usr/bin/clang++ is a wrapper program on max os X that basically runs xcrun clang++ which locates the real clang binary. We recognize this pattern when we find clang on the path ourselves, but not when /usr/bin/clang++ is explicitly specified. Maybe we need to do that too…

@thejohnfreeman best to open a new bug as the underlying issue discussed in this one is mac-specific (sorry it wasn’t labeled yet). We’ll need some more information though: does the clang++ command from compile_commands.json actually work on your system, can you provide verbose logs from clangd etc.