Troubleshooting "#include errors detected" in VS Code
Introduction
When working with Visual Studio Code (VS Code), you might occasionally encounter an error message that reads #include errors detected. Please update your includePath
. This error typically happens when the C/C++ extension in VS Code can't locate the header files included in your source code.
This Byte will guide you through understanding and resolving this issue.
The "#include errors detected. Please update your includePath" Error
The #include errors detected. Please update your includePath
error occurs when the C/C++ extension in VS Code can't find the header files referenced in your source code. The includePath
setting in VS Code defines the paths where the extension should look for header files. If the paths to your header files aren't included in the includePath
setting, you'll encounter this error.
#include <stdio.h>
#include <custom_header.h>
int main() {
// Your code here
}
In the code snippet above, if custom_header.h
isn't located in one of the paths in includePath
, you'll likely then see the #include errors detected
error.
Restarting VS Code
Sometimes, the simplest solution to this issue is just to restart VS Code. This will refresh the C/C++ extension and may resolve any temporary issues causing the error.
$ code .
The command above will close and reopen VS Code in the current directory.
Configuring C/C++ with the "Add to Include Path" Setting
VS Code has a feature to automatically configure the includePath
setting. When you hover over the #include
error, you'll see a lightbulb icon. Clicking on this icon will present you with an option to Add to Include Path
.
#include <custom_header.h> // Hover over this line
Selecting this option will automatically add the path of custom_header.h
to the includePath
setting in your .vscode/c_cpp_properties.json
file.
Adding MinGW Path to "includePath" Array on Windows
If you're using the MinGW compiler on Windows, you might need to manually add the MinGW include path to the includePath
array in the .vscode/c_cpp_properties.json
file.
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:/MinGW/lib/gcc/mingw32/9.2.0/include/c++"
],
}
],
"version": 4
}
In the JSON snippet above, replace 9.2.0
with your MinGW version. This will direct the C/C++ extension to look for header files in the MinGW include
directory, hopefully fixing the #include errors detected
error.
Updating xcode-select and Installing g++
If you're running VS Code on macOS, you might need to update xcode-select
or install g++
to resolve error. This is because VS Code uses these tools under the hood to handle C/C++ code.
First, let's update xcode-select
. Open Terminal and run the following command:
$ xcode-select --install
You'll see a software update pop-up window. Click Install
to download and install Xcode command line developer tools.
Next, let's install g++
. If you have Homebrew installed, you can use it to install g++
. Run the following command in Terminal:
$ brew install gcc
After installation, you can confirm the installation by checking the version of g++
:
$ g++ --version
You should see output similar to this, indicating that g++
is installed:
g++ (Homebrew GCC 9.2.0) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Setting the "configurationProvider"
Another common cause of the error is an incorrect configurationProvider
setting in VS Code. To fix it, you'll need to update the c_cpp_properties.json
file.
Open c_cpp_properties.json
(you can find it in the .vscode
directory at the root of your workspace), and look for the configurationProvider
field. Set its value to ms-vscode.cpptools
.
Here's an example of what your c_cpp_properties.json
file should look like:
{
"configurations": [
{
"name": "Mac",
"includePath": ["${workspaceFolder}/**"],
"defines": [],
"macFrameworkPath": ["${workspaceFolder}/**"],
"compilerPath": "/usr/bin/g++",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "macos-gcc-x64",
"configurationProvider": "ms-vscode.cpptools"
}
],
"version": 4
}
Save the file, and try building your project again.
Make Sure C/C++ Extension is Installed and Enabled
The C/C++ extension for VS Code is required for working with C/C++ code. If you're seeing the #include errors detected
error, make sure you have this extension installed and enabled.
To do this, go to the Extensions view in VS Code (you can get there by clicking on the Extensions icon in the Activity Bar on the side of the window), and search for c++
. The C/C++ extension by Microsoft should be the first result. If it's not installed, click Install
. If it's disabled, click Enable
.
Note: If you're still having trouble after trying the solutions above, I'd then recommend reaching out to the VS Code community for help. They're usually pretty helpful!
Conclusion
In this Byte, we've covered some common solutions to the #include errors detected
issue in VS Code, including updating xcode-select
and installing g++
, setting the correct configurationProvider
, and also verifying that the C/C++ extension is installed and enabled. With these steps, hopefully you can fix this issue and get back to coding.