当工作区位于 ~/src 而不是 /tmp 时,VS Code 在 "stdio.h" 下显示红色波浪线

VS Code shows red squiggles under "stdio.h" when workspace is in ~/src but not /tmp

Visual Studio 当工作区在 ~/src 中时,代码在编辑器中 #include <stdio.h> 下显示红色波浪线,但在 /tmp 中时则不会。两个工作区都编译并且 运行 代码正常。这只是编辑器中的一个错误。

我可以用一个非常简单的 Hello World C 工作区重现它。我正在使用 CMake 和 CMake 工具。版本:

目录结构如下:

% tree hello_world
hello_world
├── CMakeLists.txt
└── main.c

这是CMakeLists.txt

cmake_minimum_required(VERSION 3.0.0)
project(hello_world VERSION 1.0.0)

add_executable(hello_world main.c)

这是main.c

#include <stdio.h>

int main(int argc, char** argv)
{
    printf("hello world\n");
    return 0;
}

这是 ~/src 的屏幕截图:

下面是 /tmp 的屏幕截图:

编辑:在 Gist 上发布了两个工作区的“C/C++:日志诊断”的输出。这是差异的一部分:

     Includes:
-        /usr/local/include
         /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/12.0.0/include
-        /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include
         /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
-    Frameworks:
-        /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks

所以这解释了为什么它在一个工作区中找不到它,而不是路径不同的原因。

编辑:这是a bug in the VS Code C/C++ extension that was fixed in version 1.2.2


这显然是 FAQ:

Q: Why do I see red squiggles under Standard Library types?

A: The most common reason for this is missing include paths and defines. The easiest way to fix this on each platform is as follows:

Linux/Mac: Set "intelliSenseMode": "clang-x64" or "intelliSenseMode": "gcc-x64" and compilerPath in c_cpp_properties.json to the path to your compiler.

我只将 intelliSenseMode 更改为 clang-x64(未设置),只留下 compilerPath,这解决了问题。我不明白为什么源目录会导致“缺少包含路径”或为什么此模式不是默认模式,但现在可以使用了。

编辑:我在 microsoft/vscode-cpptools GitHub 项目上创建了 issue #7014

编辑:我的问题被骗到了issue #6992, which has been fixed in version 1.2.2