1

When compiling the following Hello World project in Visual Studio with MinGW toolchain using Ninja, iostream cannot be found by clang-tidy.

CMakeLists.txt:

cmake_minimum_required(VERSION 3.12)
project(my_project)
add_executable(my_project main.cpp)
find_program(clang_tidy_EXECUTABLE NAMES clang-tidy REQUIRED)
set_target_properties(my_project PROPERTIES CXX_CLANG_TIDY "${clang_tidy_EXECUTABLE}")

main.cpp:

#include <iostream>
int main() { std::cout << "Hello World!" << std::endl; return 0; }

In "CMake Settings" within Visual Studio, "Mingw64-Debug" profile is selected.

Clang-tidy should be able to find iostream when compiling, but it gives the following error:

error GA720FDDA: 'iostream' file not found [clang-diagnostic-error]
  #include <iostream>
           ^~~~~~~~~~

Compiling with CLion, using a different compiler (clang-cl) or disabling clang-tidy (removing last line in CMakeLists.txt) fixes the problem. Problem persists across different PCs. What could be its cause?

OLEGSHA
  • 388
  • 3
  • 13
  • I also ran into the same issue on Linux using `clang-tidy-14.0.6`, found this cmake workaround: `set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})` here: https://gitlab.kitware.com/cmake/cmake/-/issues/20912#note_793338 – mariusm Jul 11 '23 at 11:13

1 Answers1

0

I also ran into the same issue on Linux using clang-tidy 14.0.6.

It seems that clang-tidy is not able to detect the system header include path.

Found this cmake workaround:

set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})

Which adds the -isystem argument to every compiler command and clang-tidy then picks it up.

Update:

I just fixed my machine by sudo apt install libstdc++-13-dev.

The whole clang/llvm toolset (not just clang-tidy) was missing the stdc++ headers on my computer after a minor upgrade (clang-14.0.0 to clang-14.0.6 I think), so the whole compiler clang toolset was useless.

The actual change that broke it on my Debian installation was that gcc-13 got released and clang-14 somehow got configured to use libstdc++-13, but this was not installed on my computer (the default compiler is gcc-12 so I have libstdc++-12). Basically it was a Debian package missing this dependency.

Perhaps something similar is happening with MinGW.

mariusm
  • 1,483
  • 1
  • 11
  • 26