105

I'm trying to build C/C++ in Visual Studio Code. I installed C/C++ and all the relevant extensions.

#include <stdio.h>
int main() {
    printf("Test C now\n");
    return 0;
}

But there's a green line under #include <stdio.h> saying "Add include path to settings". When I click it, it moves over to "c_cpp_properties.json".

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

"configurations": [
    {
        "name": "Mac",
        "includePath": ["/usr/include"]
    }
]
AVI RAJ
  • 55
  • 1
  • 11
stdio.h
  • 1,053
  • 2
  • 8
  • 8

14 Answers14

73

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... 
}
Niall
  • 30,036
  • 10
  • 99
  • 142
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113606/discussion-between-niall-and-stdio-h). – Niall Jun 02 '16 at 09:38
  • @stdio.h. I recommend a good book on C to delve into the language; there is a list here on SO at http://stackoverflow.com/q/562303/3747990. If you are interested, there is also a C++ list; http://stackoverflow.com/q/388242/3747990. Both those lists contain at least one book available online that will get you up and going and provide a great context to a lot of the other material you find on SO and other tutorial websites. I like these list because they are curated list and not just a random dump off the internet. – Niall Jun 03 '16 at 06:58
  • 5
    I just want to add that I found the instructions to create the c_cpp_properties.json, tasks.json and launch.json here https://code.visualstudio.com/docs/languages/cpp – agwntr Jan 11 '17 at 09:00
  • 3
    Have in mind that you can use `/**` for a recursive include: `"includePath":[ "C:/path/to/all/includes/**" ]`. – Danijel Dec 07 '18 at 10:31
  • That's not a comma-delimited list (which in conventional parlance would be understood as being composed of a single string); it is an array. I concede of course that strictly speaking it is still a comma-delimited list at a certain level of abstraction :) – Lightness Races in Orbit Dec 07 '18 at 11:09
  • @LightnessRacesinOrbit, true that. Changed that – Niall Dec 07 '18 at 18:40
  • 1
    In newer versions of the C/C++ extension, it is recommended to use the `compilerPath` property to set the system includes/defines instead of adding them to the `includePath` property. – Bob Brown Mar 11 '19 at 16:58
  • This answer doesn't tell me where to put the text that begins `"configurations": [` Does that go in a specially named file somewhere? I don't get it yet. – Wyck Mar 30 '19 at 02:02
  • 1
    @Wyck. It's a property of the object of the [c_cpp_properties.json](https://github.com/Microsoft/vscode-cpptools/blob/master/Documentation/LanguageServer/c_cpp_properties.json.md) file. – Niall Mar 30 '19 at 19:19
  • where is this "configuration"? In my workspace settings there is just "settings" field. – Arkady Oct 11 '19 at 15:58
  • 1
    @arkady. It’s part of the c_cpp_properties files. If it isn’t been generated, you can create one yourself. See here for more detail https://code.visualstudio.com/docs/cpp/c-cpp-properties-schema-reference – Niall Oct 11 '19 at 19:52
  • Whenever I do this I get a warning saying "Property includePath is not allowed" and my intellisense directories are not properly updated – DebareDaDauntless Mar 27 '20 at 19:55
  • Does the `compilerPath` option require a new compiler (specifically, newer than Visual Studio 2017) to be able to determine the include path? – Ron Inbar Jul 07 '20 at 19:03
  • @RonInbar. I'm not sure, but I wouldn't be surprised if it does require a newer compiler. That option was only added later in the development and I would imagine they targeted the "current" or newer at the time of dev. If you can, it may be a case of try and see if it works. – Niall Jul 08 '20 at 12:25
  • @Niall I tried it with Visual Studio Community 2019 and it didn't work either, so it's probably not a compiler version problem. I saw [here](https://code.visualstudio.com/docs/cpp/config-msvc#_compiler-path) that you have to open Visual Studio Code from a Visual Studio Developer Prompt, which has the `INCLUDE` environment variable properly set. This solves the problem but is far from an ideal solution. – Ron Inbar Jul 09 '20 at 16:27
56

For everybody that falls off google, in here, this is the fix for VSCode 1.40 (2019):

Open the global settings.json: File > Preferences > Settings

Open the global settings.json: File > Preferences > Settings

Then select the tab 'User', open the section 'Extensions', click on 'C/C++'. Then scroll the right panel till you find a 'Edit in settings.json' button.

Then select the tab 'User', open the section 'Extensions', click on 'C/C++'. Then scroll the right panel till you find a 'Edit in settings.json' button.

Last, you add the "C_Cpp.default.includePath" section. The code provided there is from my own system (Windows 7). You can use it as a base for your own libraries paths. (Remember to change the YOUR USERNAME to your correct system (my case windows) username)
(edit info: There is a problem with the recursion of my approach. VSCode doesn't like multiple definitions for the same thing. I solved it with "C_Cpp.intelliSenseEngine": "Tag Parser" )

Last, you add the "C_Cpp.default.includePath" section. The code provided there is from my own system (Windows 7). You can use it as a base for your own libraries paths. (Remember to change the YOUR USERNAME to your correct system (my case windows) username)

the code before line 7, on the settings.json has nothing to do with arduino or includePath. You may not copy that...

JSON section to add to settings.json:

"C_Cpp.default.includePath": [
        "C:/Program Files (x86)/Arduino/libraries/**",
        "C:/Program Files (x86)/Arduino/hardware/arduino/avr/cores/arduino/**",
        "C:/Program Files (x86)/Arduino/hardware/tools/avr/avr/include/**",
        "C:/Program Files (x86)/Arduino/hardware/tools/avr/lib/gcc/avr/5.4.0/include/**",
        "C:/Program Files (x86)/Arduino/hardware/arduino/avr/variants/standard/**",
        "C:/Users/<YOUR USERNAME>/.platformio/packages/framework-arduinoavr/**",
        "C:/Users/<YOUR USERNAME>/Documents/Arduino/libraries/**",
        "${workspaceFolder}/libraries/**",
        "${workspaceFolder}/**"
    ],
"C_Cpp.intelliSenseEngine": "Tag Parser"
Vitox
  • 3,852
  • 29
  • 30
  • 1
    This did not take away the underlining of `#include ` in my case, changing `settings.json` to: `"C_Cpp.default.includePath": ["C:\\Program Files\\MariaDB\\MariaDB Connector C 64-bit\\include\\**"]` – questionto42 May 13 '20 at 16:39
  • Hi @Lorenz. Are you sure that the path to the header ( *.h ) file(s) are correct? Are you sure that it is on the "Program Files" folder and not on "Program Files (x86)" instead? Also, the slashes should be the `/` one, and not the backslash `\`. – Vitox May 13 '20 at 23:41
  • Hi @Vitox, yes, the path is correct, it is an externally installed connector for mariadb. The slash is the same as the \\ on Windows (\ is escape, thus I escape \ to actually see it). This definitely works. The linting went away when I changed the includePath in c_cpp_properties.json instead (other answers). I have documented this here: https://stackoverflow.com/questions/1985764/how-to-add-3rd-party-libraries-to-mingw/61783463#61783463 Perhaps it is also a special case? Because without adding the include and lib path as arguments, it will not compile anyway, with linting or without. – questionto42 May 14 '20 at 06:13
  • 1
    @Vitox: thank you. It's worth noting that I first added my include directories *without* adding that last line, ``"C_Cpp.intelliSenseEngine": "Tag Parser"``, and it *did not work*. After adding it, it works great. – trozzel Mar 17 '21 at 15:50
  • 1
    As of v1.55.2 (2021-04-13) the attribute `C_Cpp.default.includePath` does not seem to exist for me – coincoin Apr 16 '21 at 12:05
  • 1
    Why you have `{$workspaceFolder}` instead of `${workspaceFolder}` – Voyager Apr 28 '21 at 15:24
  • @Voyager it was a mistype. Sorry about that, and thanks for pointing it up. I updated the answer now, with the correct information. – Vitox Apr 29 '21 at 04:22
18

For Mac users who only have Command Line Tools instead of Xcode, check the /Library/Developer/CommandLineTools directory, for example::

"configurations": [{
    "name": "Mac",
    "includePath": [
            "/usr/local/include",
            // others, e.g.: "/usr/local/opt/ncurses/include",
            "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include",
            "${workspaceFolder}/**"
    ]
}]

You probably need to adjust the path if you have different version of Command Line Tools installed.

Note: You can also open/generate the c_cpp_properties.json file via the C/Cpp: Edit Configurations command from the Command Palette (⇧⌘P).

ryenus
  • 15,711
  • 5
  • 56
  • 63
  • In the current c_cpp_properties.json file, it states at the top of the file: "!!! WARNING !!!": "PLEASE DO NOT MODIFY THIS FILE! USE https://docs.platformio.org/page/projectconf/section_env_build.html#build-flags" I've confirmed also, that when adding references to this file they can get removed – Paul Allsopp Dec 06 '18 at 02:28
11

In your user settings add:

"C_Cpp.default.includePath":["path1","path2"]
Danijel
  • 8,198
  • 18
  • 69
  • 133
regomodo
  • 644
  • 3
  • 9
  • 18
  • 3
    this, with the paths provided in another answers, plus ' ** ' appended to the end, did the trick! (ex: "C_Cpp.default.includePath": [ "C:/Users/Administrator/Documents/Arduino/libraries/**" ] ) – Vitox Nov 13 '19 at 14:33
  • @Votox The real answer to this! – degski Feb 19 '20 at 20:40
  • For myself on Mac I had to be precise about where the header file was, I couldn't encapsulate with a `*` for the entire folder. For example I had a header file I needed; I had to specify the header folder for VSCode to find it: `"C_Cpp.default.includePath":[ "/Users/Github/Learning/Cpp_repo/FreeStore/header/"` – kevin_theinfinityfund May 24 '21 at 05:06
4

This answer maybe late but I just happened to fix the issue. Here is my c_cpp_properties.json file:

{
"configurations": [
    {
        "name": "Linux",
        "includePath": [
            "${workspaceFolder}/**",                
            "/usr/include/c++/5.4.0/",
            "usr/local/include/",
            "usr/include/"
        ],
        "defines": [],
        "compilerPath": "/usr/bin/gcc",
        "cStandard": "c11",
        "cppStandard": "c++14",
        "intelliSenseMode": "clang-x64"
    }
],
"version": 4

}

Gru
  • 817
  • 13
  • 20
  • `compilerPath` is the way to go. You should remove system include paths from the `includePath` property. – Bob Brown Mar 11 '19 at 16:57
  • Where is this file located on the disk? – Marc.2377 Sep 19 '19 at 04:25
  • @Bob Brown: I have not solved it with compilerPath, and I do not know how this should work at all. I am using MinGW on Windows, the compilerPath needs mingw and is a single string. The official documentation only refers to includePath in c_cpp_configurations.json: **You only need to add to the Include path array setting if your program includes header files that are not in your workspace or in the standard library path.** See: https://code.visualstudio.com/docs/cpp/config-msvc So why this answer was said to be unuseful is rather that it repeats the answer above, but it is still right. – questionto42 May 14 '20 at 06:22
3

The best way to configure the standard headers for your project is by setting the compilerPath property to the configurations in your c_cpp_properties.json file. It is not recommended to add system include paths to the includePath property.

Another option if you prefer not to use c_cpp_properties.json is to set the C_Cpp.default.compilerPath setting.

Bob Brown
  • 159
  • 5
  • Is there a _global_ `c_cpp_properties.json` file? I seem to remember there was one, under `~/.vscode/` or perhaps `~/.vscode-oss/` (on linux); now I don't see one. – Marc.2377 Sep 19 '19 at 04:21
  • 1
    No, there is no global `c_cpp_properties.json` file. You can use the `C_Cpp.default.*` settings to achieve a similar effect though. – Bob Brown Sep 20 '19 at 14:55
  • I think that this answer uses a confusing expression, perhaps it is just my bad English? I found this much better to understand, copying here @Bob Brown's comment in https://stackoverflow.com/questions/37522462/visual-studio-code-includepath: "In newer versions of the C/C++ extension, it is recommended to use the compilerPath property to set the system includes/defines instead of adding them to the includePath property." – questionto42 May 13 '20 at 14:26
2

I solved this issue by uninstalling Visual Studio that I had installed recently.

2

My c_cpp_properties.json config-

{
    "configurations": [
        {
            "name": "Win32",
            "compilerPath": "C:/MinGW/bin/g++.exe",
            "includePath": [
                "C:/MinGW/lib/gcc/mingw32/9.2.0/include/c++"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-gcc-x64"
       }
    ],
    "version": 4
}
1

I tried this and now working Configuration for c_cpp_properties.json

{
"configurations": [
    {
        "name": "Win32",
        "compilerPath": "C:/MinGW/bin/g++.exe",
        "includePath": [
            "C:/MinGW/lib/gcc/mingw32/9.2.0/include/c++"
        ],
        "defines": [
            "_DEBUG",
            "UNICODE",
            "_UNICODE"
        ],
        "cStandard": "c17",
        "cppStandard": "c++17",
        "intelliSenseMode": "windows-gcc-x64"
    }
],
"version": 4
  }

task.json configuration File

{
"version": "2.0.0",
"tasks": [
    {
        "type": "cppbuild",
        "label": "C/C++: g++.exe build active file",
        "command": "C:\\MinGW\\bin\\g++.exe",
        "args": [
            "-g",
            "${file}",
            "-o",
            "${fileDirname}\\${fileBasenameNoExtension}.exe"
        ],
        "options": {
            "cwd": "C:\\MinGW\\bin"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "detail": "compiler: C:\\MinGW\\bin\\g++.exe"
    }
]}
Mayank
  • 49
  • 5
1

In the settings, go to c_cpp_properties.json change the settings to:

enter image description here

Mohamed Bdr
  • 967
  • 3
  • 9
  • 20
0

Okay, I had a similar problem with my VS Code, and here is what I understood, Your C/C++ code runner extension has a default include path value of ${workspaceFolder}/**. This does a recursive top-down search for the library header function files from your workspace folder, this would require you to keep a copy of all your library header function files in all your working folders. I've written below the solution that worked for me.

Step:1This issue can be solved by simply right-clicking on the bulb under the squiggly line (where the error occurs) and click on the "Edit include path settings".

Step:2Now scroll down to the "Include path" section and there add the value of the path to your compiler folder and add /** to enable recursive search in all branching folders. If for any reason you have your library header files stored somewhere else add that path. You could also be more precise by adding the exact value of the folder where the header files are stored.

And that's it! now you should not get that squiggly line error anymore! No more Swiggly Lines!

0

I'm using an older version of VS Code (1.48.1). This answer solved the problem for me of a file not generating IntelliSense information (same underlying problem as in the question). If resetting the IntelliSense is not enough (Ctrl+Shift+P → C/C++: Reset IntelliSense database; it wasn't for me), try this:

Make sure there are no red squiggles (parsing errors) in the file, especially on #include directives. The best way to resolve include problems is to declare more include directories in the workspace. (I saw a lot of answers on how to do it system/user-wide, but only incomplete answers on how to do it per workspace.) Do this by generating a c_cpp_properties.json file in the workspace directory (mine didn't seem to have one already): Ctrl+Shift+P → C/C++: Edit Configurations (JSON). Add paths to includePath, until all include errors go away in the problematic file and in all files that it includes. Use the variable, ${workspaceFolder} to refer to the root directory of the workspace. By the way, mine defaulted to only having ${workspaceFolder}/** in includePath, which should recursively search your entire workspace folder for include files. It did not work for me, at least on one particular file. I had to explicitly add more directories under ${workspaceFolder}, in order for VS Code to find some includes. I didn't have any issue with standard library includes. So, this answer does not address those.

You may need to do another reset of the IntelliSense database and restart VS Code before it starts showing all IntelliSense information properly. I also found that it seemed to help to open the include files that it's looking for to help it find the file. I'm hoping this keeps working for me. So far so good.

Pulseczar
  • 150
  • 8
0

I was using MAC M1 and this error occurred while using VS code to code a c++ program. Mac has pre installed c++ compiler mainly clang not gcc. My problem occurred because I tried to include <bits/stdc++.h> header which is not supported in clang compiler. This header is used by gcc/g++ compiler only. So, to solve the problem (as in my case).

  1. If Xcode is not installed, first install it.
  2. Do not use #include<bits/stdc++.h> header. (If you still want to use this header you can do so by installing gcc compiler using homebrew or create a stdc++.h header and paste it in headers folder, refer here: How can I include <bits/stdc++> in Xcode)
Priyansh jain
  • 1,065
  • 10
  • 17
0

Reinstalling C/C++ extension pack solved this issue for me.

Shahrear Bin Amin
  • 1,075
  • 13
  • 31