Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code: How to configure includePath for better IntelliSense results

Tags:

I am a complete beginner to using Visual Studio Code and I have no clue what I am doing.

I've searched around (maybe not enough), but I can't find just a simple explanation for someone like me on how to configure the c_cpp_properties.json file that I am redirected to whenever I click on the yellow light bulb next to a line that is underlined with a green squiggle.

Lightbulb example

c_cpp_properties.json

I just want to know what to put in the .json to make IntelliSense work properly.

like image 227
Niko P. Avatar asked Sep 16 '17 20:09

Niko P.


People also ask

How do I fix my IntelliSense code in Visual Studio?

If you find IntelliSense has stopped working, the language service may not be running. Try restarting VS Code and this should solve the issue. If you are still missing IntelliSense features after installing a language extension, open an issue in the repository of the language extension.

What is includePath?

includePath An include path is a folder that contains header files (such as #include "myHeaderFile. h" ) that are included in a source file. Specify a list of paths for the IntelliSense engine to use while searching for included header files.

Why is G ++ not recognized VS Code?

If you don't see the expected output or g++ or gdb is not a recognized command, make sure your PATH entry matches the Mingw-w64 binary location where the compilers are located. If the compilers do not exist at that PATH entry, make sure you followed the instructions on the MSYS2 website to install Mingw-w64.

How do I get Visual Studio to autocomplete?

The suggestion list of Automatic completion appears as soon as you start typing a new identifier. The suggestion list of Basic completion appears when you press the default Visual Studio IntelliSense shortcut Ctrl+Space . If necessary, you can always return to the Visual Studio's native's IntelliSense.


1 Answers

From the official documentation of the C/C++ extension:

Configuring includePath for better IntelliSense results

If you're seeing the following message when opening a folder in Visual Studio Code, it means the C++ IntelliSense engine needs additional information about the paths in which your include files are located.

Configure includePath for better IntelliSense

Where are the include paths defined?

The include paths are defined in the "includePath" setting in a file called c_cpp_properties.json located in the .vscode directory in the opened folder.

You can create or open this file by either using the "C/Cpp: Edit Configurations" command in the command palette or by selecting "Edit "includePath" setting" in the light bulb menu (see the screenshot below). The quickest way to locate a light bulb is to scroll to the top of the source file and click on any green squiggle that shows up under a #include statement.

lightbulb menu "Edit "includePath" setting"

When a folder is opened, the extension attempts to locate your system headers based on your operating system, but it does not know about any other libraries that your project depends on. You can hover over the green squiggles or open the Problems window to understand which headers the IntelliSense engine is unable to open - sometimes it's the dependent headers that can't be located.

include error message

How can I specify the include paths?

You can specify the remaining paths using one of the techniques described below.

  1. Use compile_commands.json file to supply includePaths and defines information

    The extension can get the information for "includePath" and "defines" from a compile_commands.json file, which can be auto-generated by many build systems such as CMake and Ninja. Look for the section where your current configuration is defined (by default there's one configuration per operating system, such as "Win32 or "Mac"), and set the "compileCommands" property in c_cpp_properties.json to the full path to your compile_commands.json file and the extension will use that instead of the "includes" and "defines" properties for IntelliSense.

    use compileCommands setting

  2. Use the light bulb suggestions to auto-resolve includePath

    The first thing to try is to leverage the light bulb path suggestions to auto-resolve the include paths. When you open a folder, the extension will recursively search for potential include paths that match the header files your code is using based on the paths set by the "browse.path" setting in c_cpp_properties.json. Click on the green squiggles under #include statements and you'll see a light bulb offering suggestions of paths that will allow IntelliSense to resolve the included file.

    lightbulb suggestions

    If you don't see path suggestions in the light bulb, try adding the root folder where the headers are likely located in to the "browse.path" setting in c_cpp_properties.json. This allows the extension to recursively search in these folders and offer more suggestions in the light bulb as the search process goes on.

  3. Manually add include paths

    If none of the above fully resolves the paths, you could manually specify the paths to the headers that your project depends on in the c_cpp_properties.json file. Look for the section where your current configuration is defined (by default there's one configuration per OS, such as "Win32 or "Mac"), and add your paths in the "includePath" setting and defines in the "defines" setting. For example, the following screenshot shows a snippet of the file specifying path for the Mac configuration.

    Also, for MinGW, as the documentation of the extension explains you may ask gcc/g++ to list its own include files:

    gcc -v -E -x c++ nul
    

    c_cpp_properties file snippet

Verify the include paths are correctly resolved

There are two ways to verify that the include paths are correctly resolved:

  1. The green squiggles in the source file are no longer showing
  2. Error messages are cleared in the Problems window

This indicates that the IntelliSense engine has got the include paths resolved so you can start enjoying the full IntelliSense for your C or C++ code for the current translation unit. Note that you may still see errors on other files if they belong to a different translation unit that requires additional include paths to be configured.

If this didn't resolve your issue then, check out the configuration for MinGW below and try setting the appropriate location for your Cygwin installation for the respective/similar header files & folders.

Configuring MinGW

c_cpp_properties.json reference guide

like image 144
Sushant Chaudhary Avatar answered Oct 29 '22 20:10

Sushant Chaudhary