Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 328
Fillyjonk Avatar asked Jun 01 '20 14:06

Fillyjonk


People also ask

How do header files work in C?

A header file is a file with extension . h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler.

What should be in a C header file?

The header file contains only declarations, and is included by the . c file for the module. Put only structure type declarations, function prototypes, and global variable extern declarations, in the . h file; put the function definitions and global variable definitions and initializations in the .

Do C files need header files?

Without a header file, you cannot run or compile your program code. In C, all program codes must include the stdio. h header file.

Why do we write header files in C?

header files are simply files in which you can declare your own functions that you can use in your main program or these can be used while writing large C programs. NOTE:Header files generally contain definitions of data types, function prototypes and C preprocessor commands.


1 Answers

On the includePath property add ** to the end of your directories path:

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

You can look form more details about c_cpp_properties.json on the documentation

like image 68
jpuriol Avatar answered Oct 25 '22 20:10

jpuriol