8

Today I've built clang-tidy from sources, I've built it using clang++. After it has been built I've created a symlink to the executable like this:

ln -s /path/to/build/bin/clang-tidy /usr/local/bin/clang-tidy

Then I've tried to use clang-tidy with cmake on simple project (single .cpp file containing printing helloworld code). This is how my cmake file looks like:

project(Test)
cmake_minimum_required(VERSION 3.12.0 FATAL_ERROR)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
set(CMAKE_CXX_STANDARD 17)
set(CXX_STANDARD_REQUIRED)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(CMAKE_CXX_CLANG_TIDY
  clang-tidy;
  -checks=*;)

add_executable(Test
    helloworld.cpp)

I read somewhere that clang-tidy with cmake only works with Unix Makefiles & Ninja generators (or possibly some else). I usually use xcode generator, but I'm little familiar with those 2 so i didn't really care about others. I've tried to generate and build project with both Unix Makefiles and Ninja, but with both I get this error:

/Users/xxxxxxx/Dev/VSCodePlayground/helloworld.cpp:2:10: error: 'string' file not found [clang-diagnostic-error]

I found some info, that this is probably caused because clang can't find libc++/stdlib headers. So I've tried suggested compilation with -v argument (which succeeded without errors) and got this output on include dirs:

#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
 /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)

If I understand the issue is that clang-tidy doesn't know the location of headers of libc++ while clang does, am I right? But how should I resolve this and what caused this problem?

mezo
  • 423
  • 1
  • 7
  • 19
  • 1
    I don't think this is quite your problem, since you're seeing this error in a `.cpp` file, but I was running into the same issue in a `.h` file and [wrote up what I figured out](https://stackoverflow.com/a/60351985/113632); maybe it will help you. – dimo414 Feb 22 '20 at 12:10

2 Answers2

9

If I understand the issue is that clang-tidy doesn't know the location of headers of libc++ while clang does, am I right?

I happen to stumble at the very same issue. And it is really not obvious at first why compilation invoked by clang-tidy doesn't detect installed libc++.

It turns out if compile_commands.json contains symlink to clang, it will fail to find libc++. It searches relative to "installation dir", which is clang binary location, but doesn't follow symlinks. And it can't find it.

Reported here: https://bugs.llvm.org/show_bug.cgi?id=47460

As a workaround, you can pass full path to compiler to CMake so generated compile_commands.json will be "compatible" with clang-tidy. The path need to be from actual install directory, note that clang++ is still symlink to clang++ and it is important, but make sure that directory is correct.

At least this was the issue in my case on Ubuntu box. Yours looks very similar, so it is likely the same issue. Although I have no idea how XCode manages toolchains, so it might be something else entirely :)

0

Today I have tried clangd, and I got a very similar question.

Clangd tells me, 'string' file not found, 'cstring' file not found...

Finally I've fixed it. I add arg "--query-driver=/usr/bin/clang++" to clangd, and it works fine now, the headers can be found now.

C++ headers should be in folder /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1. If you install llvm by homebrew, and the folder /usr/local/Cellar/llvm/12.0.1/include/c++/v1 should be have one also.

Here is more info.

  • Editor: VS Code with ext clangd
  • OS: Mac 10.15
  • cmake
  • clang version: 12.0

Here is some links.

hideDragon
  • 344
  • 2
  • 5