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

Fil*_*onk 7 c++ clang visual-studio-code

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

\n

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

\n

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

\n

#包含错误截图

\n

c_cpp_properties.json:

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

请帮我解决这个问题。

\n

\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94更新\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\ xe2\x80\x94\xe2\x80\x94

\n

建议我在c_cpp_properties.json中使用以下设置:

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

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

\n

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

\n

安装vcpkg后,我通过vcpkg安装了wxWidgets,但该库未在 VS Code 中链接(不过构建得很好),并且出现错误花体,如下面的屏幕截图所示:

\n

请参阅波形图 \xe2\x80\x93 该库是 VS Code 的外来对象:(

\n

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

\n

Fil*_*onk 0

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

{
    "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
}
Run Code Online (Sandbox Code Playgroud)

此解决方案仅适用于 Mac。如果您在包含时遇到相同的错误,或者在不同的操作系统上出现类名波形图,请在此 GitHub 存储库中查找c_cpp_properties.json文件的属性。