Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"]     } ] 
like image 498
stdio.h Avatar asked May 30 '16 09:05

stdio.h


People also ask

What is Visual Studio Code used for?

Visual Studio Code is a streamlined code editor with support for development operations like debugging, task running, and version control. It aims to provide just the tools a developer needs for a quick code-build-debug cycle and leaves more complex workflows to fuller featured IDEs, such as Visual Studio IDE.

Is Visual Studio Code free?

Free. Built on open source. Runs everywhere. By using VS Code, you agree to its license and privacy statement.

Is Visual Studio Code good for beginners?

While marketing primarily to professional programmers, VS Code is an excellent editor for students and other learner just getting started with HTML and CSS. This course focuses mainly on those students and learners who in the beginner to intermediate stages of learning to code with HTML, CSS, and JavaScript.

Is vim better than VS Code?

When comparing Vim vs Visual Studio Code, the Slant community recommends Vim for most people. In the question“What are the best programming text editors?” Vim is ranked 1st while Visual Studio Code is ranked 3rd.


2 Answers

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...  } 
like image 99
Niall Avatar answered Sep 20 '22 08:09

Niall


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" 
like image 36
Vitox Avatar answered Sep 21 '22 08:09

Vitox