6

at the moment I am using a windows machine and wanted to try out windows subsystem for linux(wsl) for c++ development. Visual studio code(vsc) provides a neat extension for working on the subsystem that works quite well. One thing I could not get to work yet is getting vsc to include header files that are located inside the subsystem.

I know that I can manually add new paths to include in ´c_cpp_properties.json´ but I do not know any of the paths to point from windows into the subsystems /usr/include(as an example). I found some resources (wsl include paths) but could not get it to work anyway. I also tried generating the paths myself as described in the gitlab issue but that did not help either.

Anyone got some experience setting up vsc with wsl for c++ projects who would be able to help me?

A. Osterthun
  • 175
  • 1
  • 3
  • 18
  • Have you followed the guide? https://code.visualstudio.com/docs/remote/wsl – WBuck Mar 28 '20 at 03:18
  • Kinda. But I could not find information on how to set include paths on there ? Or am I just blind .. :D – A. Osterthun Mar 28 '20 at 16:52
  • I have seen that people use tools like rsync to just copy the needed header files to the local machine to point vac to them. That seems kinda tedious in my opinion. Still looking for a more integrated way. – A. Osterthun Mar 29 '20 at 03:53
  • What distro are you using in WSL, and did you install the necessary packages and VS Code extensions already? – sweenish Apr 07 '20 at 17:27

3 Answers3

1

The question is missing some information, like what distro you're using and what setup steps you have followed. This means I will make some assumptions and repeat information you may already know.

Assumption: a Debian-based distro sudo apt update && sudo apt upgrade -y && sudo apt install build-essential -y You may also need to install git or cmake or whatever other tools are needed by your project.

Install the C/C++ extension into the remote. Install any other extensions that would be beneficial, like cmake if you're using cmake.

Settings that need to be changed (global)
"C_Cpp.default.compilerPath": "/absolute/path/to/your/compiler",
"files.eol": "\n",
"C_Cpp.default.cppStandard": "<SET>",
"C_Cpp.default.cStandard": "<SET>",
"C_Cpp.default.intelliSenseMode": "<SET>",

The last three need to be set according your company/project guidelines.

At this point, you should be able to write C++ code that uses the C++ Standard Library and have proper Intellisense working.

Your main question seems to be about adding project specific include paths. That is handled by the c_cpp_properties.json file. Your hard drives are located at /mnt. Specify your paths. The better approach, though, is to simply use the VS Code variables. Something like ${workspaceFolder}/include should be all you need.

sweenish
  • 4,793
  • 3
  • 12
  • 23
  • 1
    This was how I initally tried to solve this myself. I have a folder inside the WSL that contain all of my cpp projects. When I work on them I open that folder in VSC. The "problem" with that that is, that VSC uses the configuration provided for the folder that was opened. I wrongly assumed that VSC would interpret every folder containing a .vscode folder as it's one project. When opening the project directly everything works as expected. Thanks anyway. the c_cpp_properties.json (or for that matter the .vscode folder) was not directly – A. Osterthun Apr 17 '20 at 09:10
  • Had the same issue in Windows 11, WSL, Ubuntu. Seeing "/absolute/path" above, I resolved the chain of symbolic links and set "compilerPath" to be "/usr/bin/x86_64-linux-gnu-gcc-9" Now each file that VS Code cannot find has a"+" character somewhere in its full path. – kippsoftware Jun 21 '22 at 17:56
1

When I tried using wsl and cast it on vscode. There will be error. But you still able used that library:

vscode from wsl server preview

I don't use extra configuration.

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/clang-7",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "linux-clang-x64"
        }
    ],
    "version": 4
}

I think this should be bump to WSL team.

prothegee
  • 36
  • 2
0

you need to create this file to setup the paths and more c_cpp_properties.json for more information you can take a look here https://code.visualstudio.com/docs/cpp/config-wsl#_cc-configurations

HAPPY CODING ,

Nadim Al Abdou
  • 768
  • 7
  • 13