【问题标题】:How to deal with C++ header file #include errors in VS Code on Mac?如何在 Mac 上处理 VS Code 中的 C++ 头文件#include 错误?
【发布时间】:2020-06-01 14:35:14
【问题描述】:

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

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

下面是屏幕截图和带有 VS Code 设置的 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,但库没有在 VS Code 中链接(虽然构建得很好)我得到错误曲线,如下面的屏幕截图所示:

你能解释一下如何纠正它吗?

【问题讨论】:

  • 不相关。这是默认配色方案吗?
  • 我在 Linux 上与 CMake 项目结合使用 C++ Intellisense 时遇到了类似的问题。因此我切换到了clangd并停用了C++ Intellisense。编译器使用 CMake 提供的设置,因此编译得很好,但是提供实时反馈的插件也需要获取该信息,例如通过阅读compile_commands.jsonc_cpp_properties.json 来完成,但我不确定在C++ Intellisense 的情况下在哪里以及为什么有时会失败。但是clangd 也有问题,如果你不使用 clang 构建你的代码。
  • @Evg,不,它叫“Oceanic Next Bimbo”
  • 谢谢你,@t.niese
  • 我看到的一件事是您选择 clang 11 作为 CMake 工具包,但在设置中您有 "compilerPath": "/usr/bin/g++" 可能这就是导致问题的原因。您还可以检查 VSCode 中的“输出”选项卡,那里有不同的输出,也许其中一个可以提供有关问题所在的线索。

标签: c++ visual-studio-code clang vscode-settings


【解决方案1】:

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

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

您可以在documentation 上查看有关c_cpp_properties.json 的更多详细信息

【讨论】:

  • 谢谢你,@jpuriol。不幸的是,这并不能解决问题。相反,我的 Mac 上构建的 WxWidgets 库可能存在问题:(
【解决方案2】:

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

{
    "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。如果您在包含时遇到相同的错误,或者您应该在不同的操作系统上使用类名曲线,请在 this GitHub repository 中查找 c_cpp_properties.json 文件的属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-18
    • 1970-01-01
    • 2020-04-06
    • 2021-10-21
    • 2011-01-30
    • 2020-09-09
    • 1970-01-01
    相关资源
    最近更新 更多