1

I have a simple program that compiles and runs successfully, but I can't get clang-tidy to work.

My program:

#include <iostream>

int main() {
    return 0;
}

CMakeLists.txt:

cmake_minimum_required(VERSION 3.1)
project(myProg CXX)
set(CMAKE_CXX_COMPILER "/usr/bin/clang++")
set(CMAKE_CXX_STANDARD 14)
add_executable(myProg test.cpp)

I produce a compile_commands.json using -DCMAKE_EXPORT_COMPILE_COMMANDS=ON to be used with clang-tidy:

[
{
  "directory": "/Users/me/myProj/build",
  "command": "/usr/bin/clang++ -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk -mmacosx-version-min=10.15 -o CMakeFiles/myProg.dir/test.cpp.o -c /Users/me/myProj/test.cpp",
  "file": "/Users/me/myProj/test.cpp"
}
]

This compiles and runs successfully.

Now, when I run clang-tidy -p=/Users/me/myProj/build /Users/me/myProj/test.cpp, I get this:

test.cpp:1:10: error: 'iostream' file not found [clang-diagnostic-error]

When writing this question, I realized that when I add the following line to the compile_commands.json, clang-tidy can successfully find iostream:

-I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1

Also, when I don't specify the compiler in my cmake file and I use the default compiler in /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++, clang-tidy runs with no error.

narengi
  • 1,345
  • 3
  • 17
  • 38
  • 2
    Setting `CMAKE_CXX_COMPILER` **after** the `project()` call is generally a **bad** idea. See [my answer](https://stackoverflow.com/a/63944545/3440745) for more details why this is bad. Instead, pass a compiler via command line to `cmake`. – Tsyvarev Feb 27 '21 at 00:27
  • 3.1 is ancient. Two guidelines: don't set a lower minimum version than you test on. And don't set a minimum version lower than the features you're using: `CMAKE_EXPORT_COMPILE_COMMANDS` was introduced in 3.5 – Alex Reinking Feb 28 '21 at 09:05

0 Answers0