Windows11下用WSL的原生GNU工具链来实现C系列程序的开发

事情的起因

cygwin和mingw-w64在Windows平台下始终无法解决编码问题,出现中文目录文件名可以运行但无法调试。Clion出现无法调和的bug。
故决定使用WSL获取原生的体验。

使用WSL2

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

下载安装Linux内核更新包

https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi

重启电脑
Microsoft Store安装Ubuntu22.04LTS
打开进行一些基本的配置

安装GNU工具链

sudo apt install gcc clang cmake gdb

Ubuntu库的cmake版本为3.22,而Clion需要的是3.23以上,当我安装最新的3.24又显示不支持🤣,所以下载Github编译好的3.23的包
https://github.com/Kitware/CMake/releases

wget https://github.com/Kitware/CMake/releases/download/v3.23.3/cmake-3.23.3-linux-x86_64.tar.gz
tar -zxvf cmake-3.23.3-linux-x86_64.tar.gz
# 创建软链接
sudo ln -s /mnt/c/Users/Administrator/cmake-3.23.3-linux-x86_64/bin/cmake /usr/bin/cmake

Clion添加工具链中可以自动识别已安装的WSL

在VSCode中使用WSL

  1. 可以通过Remote-WSL插件通过SSH的方式连接到WSL
  2. 通过CodeRunner等的配合
    以下为settings.json中的终端配置(必须按照terminal.integrated.defaultProfile.windows的提示书写名称)
  "terminal.integrated.profiles.windows": {
    "PowerShell": {
      "path": "pwsh.exe",
      "args": [
        "-nologo"
      ]
    },
    "Ubuntu-22.04 (WSL)": {
      "path": "wsl.exe",
      "args": []
    }
  },
  "terminal.integrated.defaultProfile.windows": "Ubuntu-22.04 (WSL)",

以下为CodeRunner的map的书写

# 必须加的一条路径指定
  "code-runner.terminalRoot": "/mnt/",
# code-runner自带的clear-output无法在终端生效
  "code-runner.executorMap": {
    "c": "clear && cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
},

以下为调试的配置的书写

~~~
因觉得不想让WSL污染本地开发环境,故停止继续研究😂

记录参考资料

Win10配置Clion+WSL2开发环境
VSC+WSL

posted @ 2022-08-11 10:36  TomZz  阅读(210)  评论(0编辑  收藏  举报