9

Basically I have some C/C++ code that I need to build and debug on a Linux machine. Unfortunately, my windows laptop doesn't have enough free hard space to install some Linux dist nor does it have enough free RAM to comfortably run VM.

Until now, I dealt with it rather comfortably using WSL, but the scale was rather small. It was easy to edit and debug 2-3 .c files through CLI and gdb, but it became really annoying on a large scale projects.

I want something simple as "edit code in windows IDE [X], compile it on remote Linux/WSL (the project uses Makefiles), and preferably debug it via gdb".

VS has something close to what I want, but it can't deal with existing Linux projects. It needs to create a new configuration which is alien to the project's Makefile.

Erica
  • 2,399
  • 5
  • 26
  • 34
Dan M.
  • 3,818
  • 1
  • 23
  • 41
  • IIRC, WSL accesses the Windows filesystems through `/mnt/c/`, `/mnt/d/`, etc. – Shawn Sep 28 '18 at 00:55
  • @Shawn yes, but it's highly discouraged to modify "shared" files. – Dan M. Sep 28 '18 at 07:58
  • An option is to place the project on Linux system and open them remotely via Sublime or Atom text editor in your Windows machine, and keep an SSH session open in parallel to debug/build project. – Arshan Sep 28 '18 at 09:20
  • @Arshan which may cause issues, since WSL expects certain metadata attached to the files and editing those on windows might lead to errors (even though they've improved situation in the latest updates i's still not recommended). – Dan M. Sep 28 '18 at 11:53

3 Answers3

7

I know this question is a bit old, but I think the solution is to make a symlink between your WSL folder and the Window's folder. This is how I handled it for a Ubuntu-20.04 WSL:

  1. Access PowerShell in Administrator mode
  2. Type cmd.exe in the PowerShell
  3. Once cmd.exe is opened, type mklink /d C:\<path_to_your_Windows_folder> \\wsl$\Ubuntu-20.04\home\<your_user>\<path_to_your_WSL_folder>

EDIT This was tested under Windows 10 Version 2004 with WSL2

  • Isn't this valid only for WSL2? Are you using WSL2 with W10 2004? (https://learn.microsoft.com/en-us/windows/whats-new/whats-new-windows-10-version-2004#virtualization) – VonC Jul 03 '20 at 12:25
  • I'm using WSL2 and W10 2004 indeed, but access to wsl folder is available since W10 1903 according to https://stackoverflow.com/a/56434613/1391121. I guess mklink will work fine as long as we have access to the wsl folder via the network interface – Luan Rios Campos Jul 03 '20 at 12:50
2

I'm unsure about C and C++ but it sounds like this is exactly the same as how i work in node and javascript every day.

I checkout my code using git inside WSL to a location like /mnt/c/code/myproject. Then using sublime/VS code/webstorm i edit the files in windows in the location c:\code\myproject this works really well and have been doing this every day for over a year.

Things to be aware of are that you need to ensure that your editor of choice saves files with linux line endings and that all command line operations are done inside WSL.

Please see this article to see the differences between windows and linux files and how this works inside the WSL.

Damo
  • 5,698
  • 3
  • 37
  • 55
2

I want something simple as "edit code in windows IDE , compile it on remote linux/WSL

You will have something as simple as that.
Only with Windows 19.03 though:

See "Updated WSL in Windows 10 version 1903 lets you access Linux files from Windows"

Microsoft's Craig Loewen says:

In the past, creating and changing Linux files from Windows resulted in losing files or corrupting data. Making this possible has been a highly requested and long anticipated feature. We're proud to announce you can now easily access all the files in your Linux distros from Windows.

So how does this work? He goes on to explain:

To put it briefly: a 9P protocol file server facilitates file related requests, with Windows acting as the client.

We've modified the WSL init daemon to include a 9P server. This server contains protocols that support Linux metadata, including permissions.
There is a Windows service and driver that acts as the client and talks to the 9P server (which is running inside of a WSL instance).
Client and server communicate over AF_UNIX sockets, since WSL allows interop between a Windows application and a Linux application using AF_UNIX as described in this post.

Warning:

The old rules still apply, you should NOT access your Linux files inside of the AppData folder!
If you try to access your Linux files through your AppData folder, you are bypassing using the 9P server, which means that you will not have access to your Linux files, and you could possibly corrupt your Linux distro.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250