VSCode使用WSL中的GCC c++編譯和GDB調試器


WSL使用:Ubuntu18.04
使用WSL而不是在Windows上安裝Mingw-64的好處,可以在Windows上得到類似的linux的部署開發經驗。

該過程分為以下幾步:

  1. 安裝VSCode
  2. VSCode安裝Remote-WSL擴展和C/C++擴展
  3. 安裝WSL並配置
  4. 配置VSCode

安裝VSCode

  1. 官網下載VSCode,如無特殊要求,默認安裝即可。

安裝Remote-WSL擴展

  1. 如圖所示,第一個插件就是

  2. 同樣,圖中第一個

安裝WSL

  1. 想要安裝WSL,
    一是需要在BIOS中打開虛擬化,
    二是 控制面板 -> 程序和功能 -> 啟用或關閉Windows功能中,打開 Hyper-V 和 適用於Linux的Windows子系統

  2. 重啟之后,直接在Microsoft Store中找到Ubuntu下載安裝即可

  3. 安裝后,打開Ubuntu,設置WSL的賬戶和密碼

    • 中科大源

      deb https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
      deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
      deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
      deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
      deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
      deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
      deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
      deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
      deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
      deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
      
    • 更換軟件源

      # 打開source.list,將文件內容替換為上述清華源,具體vim如何操作可以百度
      sudo vim /etc/apt/sources.list
      
    • 安裝gnu編譯器和gdb調試器

      sudo apt-get update
      
      sudo apt-get install build-essential gdb
      
    • 檢查g++和gdb是否安裝好

      whereis g++
      whereis gdb
      
  4. 配置VSCode

    • 在Ubuntu中,創建projects文件夾,以及在projects文件夾中創建子文件夾helloworld

      mkdir projects
      cd projects
      mkdir helloworld
      
    • 進入hello文件夾

      # 進入hello文件夾
      cd $HOME/projects/helloworld
      code .
      

    這時,VSCode會在WSL中下載並安裝一台小型服務器,然后將Windows中的VSCode與之相連。

    • 最后,再打開擴展市場搜索C/C++擴展,選擇在WSL中安裝,然后重新加載。

測試

單獨編譯和運行

  1. 在VSCode中創建helloworld.cpp文件
  • #include <iostream>
    #include <vector>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
    
        for (const string& word : msg)
        {
            cout << word << " ";
        }
        cout << endl;
    }
    
  1. 創建tasks.json文件來告訴VS Code如何構建(編譯)程序

    • 從主菜單中,選擇Terminal > Configure Default Build Task。

    • 按Ctrl + Shift + B編譯helloworld.cpp文件,生成可執行文件helloworld,成功后如下圖所示

    • 點擊圖中的加號,在WSL中以helloworld文件夾為工作目錄運行一個bash終端,此時輸入 ls,可以看到一個沒有擴展名的可執行文件helloworld,之后在bash中輸入 **./helloworld運行該文件,輸出得到:

  2. 開始調試,在彈出的菜單中選擇 C++(GDB/LLDB) -> g++ build and debug active file。

    此時會生成一個launch.json文件,之后就可以正常調試了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM