如何同步 windows 和 WSL/linux 上的代码文件?

How to synchronize code files on windows with WSL/linux?

基本上我有一些 C/C++ 代码需要在 Linux 机器上构建和调试。不幸的是,我的 windows 笔记本电脑没有足够的空闲硬盘 space 来安装一些 Linux dist,也没有足够的空闲 RAM 来舒适地 运行 虚拟机。

到目前为止,我使用 WSL 处理起来相当舒服,但规模很小。通过 CLI 和 gdb 编辑和调试 2-3 个 .c 文件很容易,但在大型项目中变得非常烦人。

我想要简单的 "edit code in windows IDE [X], compile it on remote Linux/WSL (the project uses Makefiles), and preferably debug it via gdb"。

VS 有一些接近我想要的东西,但它不能处理现有的 Linux 项目。它需要创建一个与项目的 Makefile 不同的新配置。

我不确定 C 和 C++,但听起来这与我每天在节点和 javascript 中工作的方式完全相同。

我在 WSL 中使用 git 将我的代码检出到 /mnt/c/code/myproject 这样的位置。然后使用 sublime/VS code/webstorm 我在 c:\code\myproject 位置编辑 windows 中的文件,这非常有效,并且每天都这样做已经一年多了。

需要注意的是,您需要确保您选择的编辑器保存带有 linux 行结尾的文件,并且所有命令行操作都在 WSL 中完成。

请参阅此 article 以了解 windows 和 linux 文件之间的区别以及它在 WSL 中的工作方式。

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

你会有这么简单的东西。
不过只有 Windows 19.03

参见“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.

警告:

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.

我知道这个问题有点老,但我认为解决方案是在您的 WSL 文件夹和 Window 的文件夹之间建立一个符号链接。这就是我处理 Ubuntu-20.04 WSL 的方式:

  1. 以管理员模式访问 PowerShell
  2. 在 PowerShell 中键入 cmd.exe
  3. 打开 cmd.exe 后,输入 mklink /d C:\<path_to_your_Windows_folder> \wsl$\Ubuntu-20.04\home\<your_user>\<path_to_your_WSL_folder>

编辑 这是在 Windows 10 Version 2004 和 WSL2

下测试的