#include errors detected. Please update your includePath in visual studio code - c++

I'm trying to compile C++ files in Visual Studio Code How to use C/Cpp extension and add includepath to configurations but I'm getting this errors:
#include errors detected. Please update your includePath. Squiggles are disabled for this translation unit.
cannot open source file "iostream"
I already saw this question and any of the solutions worked for me. Also I recorded a video following the steps of the visual studio code tutorial. Here is my code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" << endl;
}
And this are my C/C++ settings:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\x86_64-w64-mingw32\\include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "${default}"
}
],
"version": 4
}

open command pallete (ctrl + shift + P) then search edit configuration , select C/C++ edit configuration(UI) . Then scroll down and you will see include path , copy the location of your include path by finding it in file manager , By default the location would be C:\Program Files(x86)\mingw-w64\......\mingw32\include. Set intellisense to default. Now you are ready to enjoy your coding journey.

Got the same error.
I just disabled the C/C++ Intellisense extension of microsoft and reloaded it.

If you got this problem, Relax, u r probablu 1000th one here ;)
I have added screenshots, these are not links to some terrifying third party documentations (;
Solution
1.installation of c++ :
check you have the c++ installed, (it dosent come pre-installed! You have to do it)
If not install it. If you have installed it, navigate to it, and get the path
click on the path at top in file explorer, and Ctrl+C to copy it here is how u get the path
2.path environment variable setup
steps:
i.left click This pc(in file explorer)here
ii. manage this menu will open
iii. scroll down in the about section, find "Advanced System Settings" and follow along this
V. "edit environment variable" dialogue box will open, click new, then paste the link to the gcc compiler that you installed firstly.
Now, this path is set as environment variable, try running command : gcc --version in command panel, if still there is error, try uploading different paths of gcc.exe to the environment variable "Path". Also, re-starting pc is recommended if path variable change doesn't appear.
Now we start(or restart if it was open) the Vs-code(just a little to go)
i. Shift+Ctrl+p then : follow this
ii.then this
if the dropdown dosnt show any option, try re-starting vs-code, or pc, re-setting the environment variable step, or re-installing c++ and starting over(in worst case)
iii.scroll down the same page, and paste the path to include folders from c++ to text box corrosponding to "Include path" . you have to first get this path, search for folder with name "include", copy and add its path.(many times, ide is capable to automate this step;))
Now try running the code, it must be good to go, but iff it still throws error, try repeating process of adding environment variable, re-staqrting vscode, or even then, re-installing c++.

Related

Node addon development - cannot open source file "napi.h"

Just started fiddling around with developing an addon for Node in C++ using Node API (for the first time, I might just add). Everything is working great for the most part - I'm using the built-in compiler in Visual Studio for starting it, debugging and playing around, and node-gyp for building it for requiring it in Node. I have an entrypoint for local testing, let's call it main.cpp, where my int main() function is defined - which is executed when I run/debug my solution in Visual Studio (using Ctrl+F5/F5).
I also have a source file where the 'node communication' is defined (NodeCommunication.cpp), and it is this file (among others) that is listed in the sources array in my binding.gyp file. It is here that napi.h is included as well, and its APIs are used. Mousing over the include however, there is error text saying "cannot open source file napi.h" - which makes sense, I guess (?) that header file is something that is only injected on build time in node-gyp somehow. However it'd be really nice, DX-wise, to be able to resolve those somehow, because right now I have to comment out all the source code in that file before compiling and running the solution in visual studio for testing purposes.
What are my options here? Might as well also attach my binding.gyp file for reference.
{
"targets": [
{
"target_name": "foobar",
"sources": [ "NodeCommunication.cpp", "Sample.cpp", "Sample.h" ],
"libraries": ["../SDK/Library/EDSDK.lib"],
'include_dirs': ["<!#(node -p \"require('node-addon-api').include\")"],
'dependencies': ["<!(node -p \"require('node-addon-api').gyp\")"],
'defines': [ 'NAPI_CPP_EXCEPTIONS' ]
}
]
}

VSCode C/C++ IntelliSense ignore patterns for includePath and browse.path

Is there a feature in VSCode IntelliSense that allows to ignore specific header files for includePath and browse.path settings?
In my case I have a CMake based firmware project with 2 active compilers. Project includes mocks and tests for existing modules, as such in some cases "Go to definition" or F12 will jump to mocks instead of actual implementations. I want to be able to ignore anything under test/* pattern when figuring our which header to jump to.
In addition the converse is also difficult, it seems I cannot specify any wildcards other than at the very end of the path attributes?
{
"configurations": [
{
"includePath": [
"${workspaceFolder}/*/inc/**",
"${workspaceFolder}/*/api/**",
],
...
}
Which isn't a valid pattern since it expands to ${workspaceFolder}//inc

VSCode C++ IntelliSense works fine except for PCL (Point Cloud Library)

On SO itself, there are already many questions about VSCode IntelliSense, e.g., que1, que2, que3, etc. However, they mainly talk about IntelliSense not working in general; not working for STL; and so on.
On the other hand, in my case, I'm already using VSCode C/C++ Extension by Microsoft and IntelliSense works fine for all the different in-built classes, functions, STLs, etc. But, when it comes to PCL (Point Cloud Library), somehow, mysteriously, IntelliSense doesn't work.
If you are familiar with PCL then must be knowing that most of its syntaxes are too big. And as the IntelliSense not working for it, there are very high chances of making typos if you don't pay close attention while typing every single character, e.g.,
pcl::PointCloud<pcl::PointXYZRGB>::Ptr point_cloud_ptr (new pcl::PointCloud<pcl::PointXYZRGB>);
In fact, auto-completion for even those variables and functions do not work--i.e., from the above example, I have to manually type every single character of point_cloud_ptr every time I want to use it.
So, how can I fix this? or it's not possible for PCL?
Note: I'm facing this issue on my Ubuntu 16 as well as on Ubuntu 18. I have installed PCL using sudo apt install libpcl-dev and the header files are located in /usr/include/ directory.
After trying out different things and getting help from Microsoft VSCode (and C/C++ extension) team on GitHub, it looks like the IntelliSense is not working probably because the extension is not able to find PCL headers.
In the case of Ubuntu, the PCL headers are generally stored at /usr/include/pcl-<version>. In my Ubuntu 18.04, I have pcl-1.8. So, ideally, adding "/usr/include/pcl-1.8/**" under "includePath" in c_cpp_properties.json file should work. e.g.
{
"configurations": [
{
.
.
"includePath": [
"${workspaceFolder}/**",
"/usr/include/pcl-1.8/**"
],
.
.
.
}
],
"version": 4
}
In "/usr/include/pcl-1.8/**", ** is for recursive search under /usr/include/pcl-1.8/ directory. However, by the time when I had posted the question (and I think as of today as well), it doesn't work. It's not able to go through the subdirectories under pcl-<version> directory. So, alternate solutions are below:
Solution 01: (Use this one if you want to apply changes only to the current project)
Open C/C++ Extension Configuration file: c_cpp_properties.json (On Linux, press Ctrl + Shift + P and select C/C++: Edit Configurations (JSON))
Now, as the recursive search is not working, an alternate way is to specify all the subdirectories under includePath:
{
"configurations": [
{
.
.
"includePath": [
"${workspaceFolder}/**",
"/usr/include/eigen3",
"/usr/include/pcl-1.8",
"/usr/include/pcl-1.8/pcl",
"/usr/include/pcl-1.8/pcl/2d",
"/usr/include/pcl-1.8/pcl/apps",
"/usr/include/pcl-1.8/pcl/common",
"/usr/include/pcl-1.8/pcl/compression",
"/usr/include/pcl-1.8/pcl/console",
"/usr/include/pcl-1.8/pcl/features",
"/usr/include/pcl-1.8/pcl/filters",
"/usr/include/pcl-1.8/pcl/geometry",
"/usr/include/pcl-1.8/pcl/impl",
"/usr/include/pcl-1.8/pcl/in_hand_scanner",
"/usr/include/pcl-1.8/pcl/io",
"/usr/include/pcl-1.8/pcl/kdtree",
"/usr/include/pcl-1.8/pcl/keypoints",
"/usr/include/pcl-1.8/pcl/ml",
"/usr/include/pcl-1.8/pcl/modeler",
"/usr/include/pcl-1.8/pcl/octree",
"/usr/include/pcl-1.8/pcl/outofcore",
"/usr/include/pcl-1.8/pcl/people",
"/usr/include/pcl-1.8/pcl/range_image",
"/usr/include/pcl-1.8/pcl/recognition",
"/usr/include/pcl-1.8/pcl/registration",
"/usr/include/pcl-1.8/pcl/ros",
"/usr/include/pcl-1.8/pcl/sample_consensus",
"/usr/include/pcl-1.8/pcl/search",
"/usr/include/pcl-1.8/pcl/segmentation",
"/usr/include/pcl-1.8/pcl/stereo",
"/usr/include/pcl-1.8/pcl/surface",
"/usr/include/pcl-1.8/pcl/tracking",
"/usr/include/pcl-1.8/pcl/visualization"
],
.
.
.
}
],
"version": 4
}
Solution 02: (Use this one if you want to apply changes globally -- across all the projects)
As C/C++ Configuration File: c_cpp_properties.json is specific to each project, editing that will only apply changes to the current project. So, to apply changes across all the projects, one has to update settings.json and then c_cpp_properies.json.
Open settings.json and add the following text:
"C_Cpp.default.includePath": [
"/usr/include/eigen3",
"/usr/include/pcl-1.8",
"/usr/include/pcl-1.8/pcl",
"/usr/include/pcl-1.8/pcl/2d",
"/usr/include/pcl-1.8/pcl/apps",
"/usr/include/pcl-1.8/pcl/common",
"/usr/include/pcl-1.8/pcl/compression",
"/usr/include/pcl-1.8/pcl/console",
"/usr/include/pcl-1.8/pcl/features",
"/usr/include/pcl-1.8/pcl/filters",
"/usr/include/pcl-1.8/pcl/geometry",
"/usr/include/pcl-1.8/pcl/impl",
"/usr/include/pcl-1.8/pcl/in_hand_scanner",
"/usr/include/pcl-1.8/pcl/io",
"/usr/include/pcl-1.8/pcl/kdtree",
"/usr/include/pcl-1.8/pcl/keypoints",
"/usr/include/pcl-1.8/pcl/ml",
"/usr/include/pcl-1.8/pcl/modeler",
"/usr/include/pcl-1.8/pcl/octree",
"/usr/include/pcl-1.8/pcl/outofcore",
"/usr/include/pcl-1.8/pcl/people",
"/usr/include/pcl-1.8/pcl/range_image",
"/usr/include/pcl-1.8/pcl/recognition",
"/usr/include/pcl-1.8/pcl/registration",
"/usr/include/pcl-1.8/pcl/ros",
"/usr/include/pcl-1.8/pcl/sample_consensus",
"/usr/include/pcl-1.8/pcl/search",
"/usr/include/pcl-1.8/pcl/segmentation",
"/usr/include/pcl-1.8/pcl/stereo",
"/usr/include/pcl-1.8/pcl/surface",
"/usr/include/pcl-1.8/pcl/tracking",
"/usr/include/pcl-1.8/pcl/visualization"
]
Open, c_cpp_properties.json and update includePath:
{
"configurations": [
{
.
.
"includePath": [
"${workspaceFolder}/**",
"${default}"
],
.
.
.
}
],
"version": 4
}
P.S. I had asked the same question on the official GitHub issues page of the C/C++ Extension by Microsoft and eventually, found the solution (but, not an ideal one) I mentioned above.
Here is the link for more info: https://github.com/microsoft/vscode-cpptools/issues/5843
Because VSCode always told me that it could not find some dependency like pcl/PCLHeader.h. So I tried copying /usr/include/pcl-1.10/pcl to /usr/include/ and it worked.

how to add include path for language servers in coc-settings.json file?

I am using neovim plugin "coc-nvim", as the IntelliSense tool for C/C++. as I know this plugin is actually a client of language servers like ccls, clangd and cquery. I have all of these language servers installed on my system and configured them to work with coc-nvim. so I can get autocompletion and list of general headers(e.g. stdio and ...) in my c/c++ files. but since I work in ros platform I want to add the headers of the ros(/opt/ros/kinetic/include/**) to my default include path of language servers. with out this option, at the beginning of each project I must add a dummy ros node and compile with catkin_make -DCMAKE_EXPORT_COMPILE_COMMANDS=YES to generate the compile_commands.json file to get access to ros headers.
the settings of coc.nvim is alongside the init.vim file, in a file called "coc-settings.json" and the contents of my coc setting file is as below:
{
"languageserver": {
"clangd": {
"command": "clangd",
"rootPatterns": ["compile_flags.txt", "compile_commands.json"],
"filetypes": ["c", "cpp", "objc", "objcpp"]
},
"cquery": {
"command": "/home/amirrezasadeghi/ManBuildLibs/cquery/build/cquery",
"args": ["--log-file=/tmp/cq.log"],
"filetypes": ["c", "cpp"],
"rootPatterns": ["compile_flags.txt", "compile_commands.json", ".git/", ".hg/"],
"initializationOptions": {
"cacheDirectory": "/tmp/cquery"
}
},
"ccls": {
"command": "ccls",
"filetypes": ["c", "cpp", "objc", "objcpp"],
"rootPatterns": [".ccls", "compile_commands.json", ".git/", ".hg/"],
"initializationOptions": {
"cache": {
"directory": "/tmp/ccls"
}
}
}
},
"clangd.semanticHighlighting": true
}
So my major question is how to add ros include path to these settings. I checked the wiki page of coc.nvim but can't find any option for user include path. also, I checked the option of clangd and tried to add,
"args":["-I/opt/ros/kinetic/include/"],
to the corresponding block but it did not work. also, I added .cquery file, but still no effect on the list of the proposed headers.
sorry for long story, and thanks for your time.
Edit: it would be better to use the clangd LSP rather than ccls. because it is faster and has better documentation. thanks!
Edit(12 April 2020): the best solution which I found is using ccls language server by using the extraArgs which is provided by ccls. so if you add "clang" field to ccls configs we can have ros headers in autocompletion.
"initializationOptions": {
"cache": {
"directory": "/tmp/ccls"
},
"clang": {
"extraArgs": [
"-I",
"/opt/ros/kinetic/include/"
]
}
}
but as I mentioned I would like to use clangd if possible. I checked everywhere but no answer. can anyone help me? (please!).
This doesn't fix the problem in the way you asked, but I found it to be a useful solution.
Like you, I used catkin_make to generate the compile_commands.json using:
catkin_make --cmake-args -DCMAKE_EXPORT_COMPILE_COMMANDS=1
However, I created a symlink between <catkin_ws>/build/compile_commands.json and <catkin_ws>/compile_commands.json. Since clangd searches parent directories for compile_commands.json, this is accessible by any package/node within the workspace.
Therefore, only one compile_commands.json needs to be generated for the whole workspace, and only needs to be re-generated whenever dependencies change.
It looks like there is no option in coc-nvim for setting that.
Best option is to rely on your LSP options.
I just solved it for ccls by defining a custom .ccls file and added a -I option pointing at my boost includes. That .ccls file must be at the root of my project. e.g.:
clang
%c
-I/usr/local/include
There must be the same option for clangd.

#include errors detected in vscode

I am using Visual Studio Code in my C++ project. I installed Microsoft C/C++ Extension for VS Code. I got the following error:
#include errors detected. Please update your includePath. IntelliSense features for this translation unit (/path/to/project/file.cpp) will be provided by the Tag Parser.
Close and re-open Visual Studio Code.
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 🖰 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.
If you are working with cmake-tools and the error messages says something is wrong with the configurationProvider, then you have 2 options:
Use ms-vscode.cpptools instead of ms-vscode.cmake-tools
Define the headers in CMakeLists.txt
Option 1: Use ms-vscode.cpptools instead of ms-vscode.cmake-tools.
Open c_cpp_properties.json. (windows key on windows or cmd key on mac + shift + p, enter "c/c++ edit configurations" and chose 'json'.
Enter ms-vscode.cpptools as value for configurationProvider instead of ms-vscode.cmake-tools or whatever you have.
How it should look like after the replacement of configurationProvider:
One other important configuration is the include path. The assumption is that you have this configuration right. May be like following
Option 2: Define the headers in CMakeLists.txt
When configurationProvider is set to ms-vscode.cmake-tools in c_cpp_properties.json, VS Code uses the include information defined in the CMakeLists.txt instead of reading the includePath configs in VS Code.
So you need to setup the include path correctly:
using the include_directories command (rather than the target_include_directories command) to define the headers
Configure the project to reflect the change happened in the previous step.
Left mouse click on the bulb of error line
Click Edit Include path
Then this window popup
Just set Compiler path
I ended up here after struggling for a while, but actually what I was missing was just:
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.
source: https://code.visualstudio.com/docs/languages/cpp#_intellisense
The error message "Please update your includePath" does not necessarily mean there is actually a problem with the includePath. The problem may be that VSCode is using the wrong compiler or wrong IntelliSense mode. I have written instructions in this answer on how to troubleshoot and align your VSCode C++ configuration with your compiler and project.
I'm on a Macbook M1 Pro, and I had red squiggly error lines all over my C++ files. To solve, I did this:
Open Preferences -> Settings
Search for intelliSenseEngine
Change "C_Cpp: Intelli Sense Engine" from Default to Tag Parser
alternatively, you could create a .vscode folder in your project root, and then create a settings.json with the content of
{
"C_Cpp.intelliSenseEngine" : "Tag Parser"
}
ideally, you should have a c_cpp_properties.json file in the same folder with the right settings as well.
Note: I got this idea from Github here.
I was trying a hello world program, and this line:
#include <stdio.h>
was underlined green. I tried:
Deleting the line
Re-writing the line
Clicking the yellow bulb and choosing to update
fixed the error warning. i don't know if it fixed the actual problem. But then i'm compiling via a linux VM on Windows 10
Go to your c_cpp_properties.json file by searching from settings.There you might see the following code
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "c++17",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
Change the compiler path as below
"compilerPath": "/usr/bin/g++",
After closing and reopening VS, this should resolve.
For Windows:
1.Install Mingw-w64
2.Then Edit environment variables for your account "C:\mingw-w64\x86_64-8.1.0-win32-seh-rt_v6-rev0\mingw64\bin"
3.Reload
For MAC
1.Open search ,command + shift +P, and run this code “c/c++ edit configurations (ui)”
2.open file c_cpp_properties.json and update the includePath from "${workspaceFolder}/**" to "${workspaceFolder}/inc"
If someone have this problem, maybe you just have to install build-essential.
apt install build-essential
For me, using Ubuntu, I just had to install gcc to solve this issue.
sudo apt install gcc
Then, set the compiler path to gcc. Go to your c_cpp_properties.json file, set:
"compilerPath": "/usr/bin/gcc"
An alternative answer would be opening VS Code in remote WSL, if you going to compile files with g++. Just close your VS Code and open WSL and type code . After that the File Explorer shows that VS Code is now running in the context of WSL with the title bar [WSL: Ubuntu]. But make sure you'd installed the GNU compiler tools and the GDB debugger on WSL.
source: https://code.visualstudio.com/docs/cpp/config-wsl
In my case I did not need to close the whole VS-Code, closing the opened file (and sometimes even saving it) solved the issue.
I had luck removing the comments from c_cpp_properties.json in the .vscode folder. Comments aren't permitted in json files by default and you can't simply rename it .jsonc. Referenced In VS Code, disable error "Comments are not permitted in JSON"
My header file was in include/head.h, code in src/code.cpp. I wrote
#include "head.h"
and got this error. Changing it to
#include "../include/head.h"
fixed it.
I solved this problem, once I removed configurationProvider node and set
cStandard and cppStandard to default values:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}",
"${workspaceFolder}/**",
"${workspaceFolder}/test",
"/opt/qt5/include/QtCore",
"${workspaceFolder}/test"
],
"defines": [],
"compilerPath": "/usr/bin/clang",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "linux-clang-x64"
}
],
"version": 4
}
after you install the c/c++ extension, two files are created inside .vscode folder.
open c_cpp_properties.json file and paste this key-value pair inside configuration object.(if it doesn't already exists)
"configurationProvider": "ms-vscode-cpptools"
if it does already exists in the object, see if the value part is ms-vscode-cmaketools. if such it is, replace that existing line with above line.
this will allow you to execute your cpp files along with c files.
I solved the error on my Mac by just clicking on the Edit "include path settings" and changing the compiler path to /usr/bin/clang.
For Windows:
Please add this directory to your environment variable(Path):
C:\mingw-w64\x86_64-8.1.0-win32-seh-rt_v6-rev0\mingw64\bin\
For Include errors detected, mention the path of your include folder into
"includePath": [
"C:/mingw-w64/x86_64-8.1.0-win32-seh-rt_v6-rev0/mingw64/include/" ]
, as this is the path from where the compiler fetches the library to be included in your program.
In case you've copied and pasted code into your new file in VS Code
Please delete #include <iostream> and try to build again.