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...
}
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
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"
)
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"
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 theC/Cpp: Edit Configurations
command from the Command Palette (⇧⌘P).
In your user settings add:
"C_Cpp.default.includePath":["path1","path2"]