Visual Studio Code, #Include ≪Stdio.H≫ Saying "Add Include Path to Settings"

Visual Studio Code, #include <stdio.h> saying Add include path to settings

A more current take on the situation. During 2018, the C++ extension added another option to the configuration compilerPath of the c_cpp_properties.json file;

compilerPath (optional)
The absolute path to the compiler you use to build your project. The extension will query the compiler to determine the system include paths and default defines to use for IntelliSense.

If used, the includePath would not be needed since the IntelliSense will use the compiler to figure out the system include paths.


Originally,

How and where can I add include paths in the configurations below?

The list is a string array, hence adding an include path would look something like;

"configurations": [
{
"name": "Mac",
"includePath": ["/usr/local/include",
"/path/to/additional/includes",
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include"
]
}
]

Source; cpptools blog 31 March 2016.

The linked source has a gif showing the format for the Win32 configuration, but the same applies to the others.

The above sample includes the SDK (OSX 10.11) path if Xcode is installed.

Note I find it can take a while to update once the include path has been changed.

The cpptools extension can be found here.

Further documentation (from Microsoft) on the C++ language support in VSCode can be found here.


For the sake of preservation (from the discussion), the following are basic snippets for the contents of the tasks.json file to compile and execute either a C++ file, or a C file. They allow for spaces in the file name (requires escaping the additional quotes in the json using \"). The shell is used as the runner, thus allowing the compilation (clang...) and the execution (&& ./a.out) of the program. It also assumes that the tasks.json "lives" in the local workspace (under the directory .vscode). Further task.json details, such as supported variables etc. can be found here.

For C++;

{ 
"version": "0.1.0",
"isShellCommand": true,
"taskName": "GenericBuild",
"showOutput": "always",
"command": "sh",
"suppressTaskName": false,
"args": ["-c", "clang++ -std=c++14 -Wall -Wextra -pedantic -pthread \"${file}\" && ./a.out"]
}

For C;

{ 
"version": "0.1.0",
"isShellCommand": true,
"taskName": "GenericBuild",
"showOutput": "always",
"command": "sh",
"suppressTaskName": false,
"args": ["-c", "clang -std=c11 -Wall -Wextra -pedantic -pthread \"${file}\" && ./a.out"] // command arguments...
}

#include errors detected in vscode

The answer is here: How to use C/Cpp extension and add includepath to configurations.

Click the light bulb and then edit the JSON file which is opened. Choose the right block corresponding to your platform (there are Mac, Linux, Win32 – ms-vscode.cpptools version: 3). Update paths in includePath (matters if you compile with VS Code) or browse.paths (matters if you navigate with VS Code) or both.

Thanks to @Francesco Borzì, I will append his answer here:

You have to Left /kbd> click on the bulb next to the squiggled code line.

If a #include file or one of its dependencies cannot be found, you can also click on the red squiggles under the include statements to view suggestions for how to update your configuration.

Sample Image

Visual Studio Code include file not found in include directory (Windows 10)

  1. First, make sure to create a c_cpp_properties.json file in your .vscode folder

    Hint: Use the Command Palette (Ctrl+Shift+P) and type C/Cpp: Edit Configurations

  2. Add include paths like this:

    {
    "configurations": [
    {
    "name": "Win32",
    "includePath": [
    "path_to_your/MinGW/lib/gcc/mingw32/4.8.1/include/c++"
    ],
    "browse": {
    "limitSymbolsToIncludedHeaders": true,
    "databaseFilename": ""
    }
    }
    ]
    }

Workspace-specific includePath for Visual Studio Code

The c_cpp_properties.json file described in that question is in the .vscode folder in your workspace, so it is already project specific.

If you use the "Add include path to settings" feature the extension will create a c_cpp_properties.json file with the system default paths, but that is only to help you get started, it does not mean that is a global configuration.



Related Topics



Leave a reply



Submit