Search code examples
c++clangvisual-studio-code

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


VS Code on my Mac yields #include errors for header files and third-party libraries (wxWidgets in this case). I read everything I could find, adjusted "includePath" settings in "c_cpp_properties.json", but nothing helps.

Header files are located in the same folder as .cpp files ("/src/"). The project builds and runs nicely, but VS Code yields #include errors and error squiggles cover my entire project.

Below is the screenshot and a JSON file with VS Code settings.

#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
}

Please help me straighten this out.

————— UPDATE —————

I was recommended to use the following settings in 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
}

Header file #include errors are gone, but the third-party library ("WX") errors persist. In the JSON above, there is the line "${vcpkgRoot}/x64-osx/include" written in the "includePath".

This is the vcpkg package which helps install third-party libraries easily.

After installing vcpkg, I installed wxWidgets via vcpkg, but the library isn't linking in VS Code (builds just fine though) and I get error squiggles as shown on the screenshot below:

see squiggles – the library is an alien object for VS Code :(

Could you please explain how to straighten it out?


Solution

  • The origin of the problem was in several environment variables in defs.h file of WxWidgets library. To make VS Code recognize them on my system (OS X 10.14 Mojave), I had to add "defines" in the c_cpp_properties.json file in the following way:

    {
        "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
    }
    

    This solution is valid for Mac only. If you experience the same errors with includes or should you have class name squiggles on a different operating system, please find properties for c_cpp_properties.json file in this GitHub repository.