Is there a way to use my Linux /usr/include directory as the include path in Visual Studio Code which is run from Windows?
Note: There is a similar question on Stack Overflow: How to set include paths within Windows Subsystem for Linux in Visual Studio Code However this question was not answered, and it does not answer my question. I prefer my question to not be linked to that one, because I believe I worded my question better, and because I believe the old thread might not get replies anymore, and I do want to get an answer to this question.
I'm using a Windows 10 machine for development. I'm writing C++ code that is meant to be compiled on Linux. I have WSL2 with Ubuntu 22 installed on my Windows 10 development machine. I'm using Visual Studio Code as an IDE, installed on my Windows segment. I have gcc and g++ installed on Ubuntu 22 in the WSL2 segment.
My code compiles and runs successfully (on Ubuntu). The only problem is that during the development process, Visual Studio Code gives error squiggles for include paths for system includes such as, for example:
#include <sys/stat.h>
Although this is not a valid include path for Windows, it is a valid include path on my target Linux environment. I want those error squiggles to go away, and to be able to Ctrl+LeftClick on those include paths and see the source of these header files, just like Visual Studio Code allows to do for any other include path it detects.
I can define which include paths Visual Studio code uses for my project by creating a .vscode directory inside my workspace directory, and then create a c_cpp_properties.json inside .vscode with contents such as:
{
"configurations": [
{
"name": "default",
"includePath": [
"${workspaceFolder}/hpp",
"C:/wsl_ubuntu_usr_include",
"C:/wsl_ubuntu_usr_include/x86_64-linux-gnu"
]
}
]
}
This tells Visual Studio Code to use the hpp directory in my workspace directory as an include path, as well as a directory called wsl_ubuntu_usr_include in my C drive, and a subdirectory of that called x86_64-linux-gnu.
As a temporary workaround, I have created the directory wsl_ubuntu_usr_include by copying the contents of \\wsl.localhost\Ubuntu\usr\include
which is the path on Windows to access the /usr/include of my Ubuntu 22 installation on WSL2.
What I really want to do is:
Use \\wsl.localhost\Ubuntu\usr\include
as the include path in .vscode\c_cpp_properties.json, or a functional equivalent. I tried using \\wsl.localhost\Ubuntu\usr\include
directly, but it didn't work, I was still getting error squiggles and could not Ctrl+LeftClick linux include paths. This is why I ended up copying /usr/include to Windows and setting that as an include path as a temporary workaround. I don't like this workaround, because it requires me to have duplicate files on my PC, and because I have to remember to re-dupe those again every time I install anything new which creates or edits files in /usr/include. (e.g. I update boost with apt upgrade
, now I have to dupe again).
Is there a way to use my Linux /usr/include directory as the include path in Visual Studio Code which is run from Windows?
Note: I'm aware that I can just run code
from within the WSL2 Ubuntu, but I don't want to do this, because then #include <windows.h>
won't work, creating the opposite problem. My code has define flags that guard various implementations for both Windows and Linux, and I want it to compile on both, but this is not relevant for this question. For the sake of this question, I want to run my IDE from Windows, not from the WSL2 Linux environment.