如何处理 Mac 上 VS Code 中的 C++ 头文件 #include 错误?

How to deal with C++ header file #include errors in VS Code on Mac?

我的 Mac 上的 VS Code 为 头文件 第三方生成 #include 错误(在本例中为wxWidgets)。我阅读了我能找到的所有内容,调整了“c_cpp_properties.json”中的“includePath”设置,但没有任何帮助。

头文件与 .cpp 文件位于同一文件夹中(“/src/”)。该项目构建和运行良好,但 VS Code 产生 #include 错误并且错误波浪线覆盖了我的整个项目。

下面是屏幕截图和带有 VS 代码设置的 JSON 文件。

#include error screenshot

c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/src",
                "${workspaceFolder}/**",
                "/usr/local/Cellar/wxmac/3.0.5.1/include/wx-3.0"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/g++",
            "cStandard": "c11",
            "cppStandard": "c++17"
        }
    ],
    "version": 4
}

请帮我解决这个问题。

—————— 更新 ——————

我在c_cpp_properties.json中被推荐使用以下设置:

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**",
                "${vcpkgRoot}/x64-osx/include",
                "/usr/local/Cellar/wxmac/3.0.5/**"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64",
            "configurationProvider": "ms-vscode.cmake-tools"
        }
    ],
    "version": 4
}

头文件#include错误消失了,但是第三方库(“WX”)错误仍然存​​在。在上面的JSON中,在“includePath”中写了一行“${vcpkgRoot}/x64-osx/include”。

这是 vcpkg 包,可帮助轻松安装第三方库。

安装 vcpkg 后,我通过 vcpkg 安装了 wxWidgets,但是库没有t 在 VS Code 中链接(虽然构建得很好)并且我得到错误波形图,如下面的屏幕截图所示:

能否请您解释一下如何理顺它?

includePath 属性 中将 ** 添加到目录路径的末尾:

...
"includePath": [
    "${workspaceFolder}/src/**",
    "${workspaceFolder}/**",
    "/usr/local/Cellar/wxmac/3.0.5.1/include/wx-3.0/**"
 ],

您可以在 documentation

上查看有关 c_cpp_properties.json 的更多详细信息

问题的根源在于WxWidgets库的defs.h文件中的几个环境变量。为了让 VS Code 在我的系统 (OS X 10.14 Mojave) 上识别它们,我必须在 c_cpp_properties.json[ 中添加 "defines" =20=] 文件如下:

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/src/**",
                "/usr/local/include/wx-3.1/**",
                "/usr/local/lib/wx/include/osx_cocoa-unicode-3.1",
                "/usr/local/lib/wx/**"
            ],
            "defines": [
                "WX_PRECOMP",
                "__WXOSX_COCOA__",
                "_FILE_OFFSET_BITS=64",
                "__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES=1"                
            ],
            "forcedInclude": [],
            "macFrameworkPath": [],
            "compilerPath": "/usr/local/bin/gcc-9",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}

此解决方案仅对 Mac 有效。如果您在使用 include 时遇到相同的错误,或者如果您在不同的操作系统上有 class 名称波浪线,请在 [=11= 中查找 c_cpp_properties.json 文件的属性].