使用Visual Studio Code開發(編譯、調試)C++程序(Java)


使用Visual Studio Code開發(編譯、調試)C++程序

先放結論

經過以下折騰,可以使用VSC編寫C++程序了。
但如果只是為了學習C、C++、數據結構,還是建議直接使用Visual Studio Community。不用折騰配置,功能更強大,易用性更好。
如果只是編寫一些小程序,用CodeBlock也很香。

安裝步驟

  1. 安裝VSC(Visual Studio Code)
  2. 安裝C/C++編譯器(如MinGW-w64),然后配置好環境變量。
    • 下載:打開MinGW-w64,選擇合適版本,如x86_64-posix-seh進行下載。
    • 配置操作系統的Path變量:在Path變量中加入mingw的安裝路徑,如d:\mingw64\bin\
    • 配置完即可在VSC的終端或系統命令行下編譯、運行.cpp程序了。參考命令:g++ HelloWorld.cpp -o HelloWorld
  3. 安裝並配置C++ Intellisense
  4. 安裝並配置Code Runner插件,可在VSC內一鍵編譯運行
    • 打開VSC中的擴展管理界面(文件-首選項-擴展,或者Ctrl + Shift + x),搜索Code Runner
      • 快速編譯並運行當前文件:Ctrl + Alt + N或右鍵點擊當前編輯框、選擇Run Code
    • 配置控制台允許輸入:在"文件-首選項-設置-用戶-擴展-Run Code Configuration",勾選Run In Terminal
  5. 解決中文亂碼問題
    • 配置:"文件-首選項-設置",搜索encoding,勾選"Auto Guess Encoding"。
  6. 配置調試環境(可選)
    • 需配置launch.json與task.json
    • 創建launch.json:按Ctrl+Shift+P,輸入Tasks: Configure Task,選擇Create tasks.json file from templates,選擇Others
    • 創建task.json:好像按F5就會提示創建。

task.json(用於Build)

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build hello",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g","${file}","-o","${fileDirname}/${fileBasenameNoExtension}.exe"  //代表build的是當前文件
            ],
            "group": {                   //該配置:按ctrl+shift+b就可以直接build當前文件
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

launch.json(用於調試)

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "d:/mingw64/bin/gdb.exe",  //mingw的調試程序所在路徑
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true                    
                }
            ],
            "preLaunchTask": "build hello" //要與tasks.json中的label屬性值一樣
        }
    ]
}

常用快捷鍵與其他設置

快捷鍵

命令面板(重要):Ctrl+Shift+P
自動生成小代碼塊:Tab
對同一個文件並排打開多個:Ctrl+\
注釋行:Ctrl+/
刪除行:Ctrl+Shift+K
格式化文本:Shift+Alt+F或右鍵點擊編輯區。
列選擇:Shift+Alt+鼠標拖拽

注意:如果想使用其他編輯上的慣用快捷鍵,請選擇文件-首選項-按鍵映射選擇相應的方案。

其他設置

使用鼠標滾輪改變字體大小:打開文件-首選項-設置搜索設置中輸入mousewheelzoom,勾選Editor: Mouse Wheel Zoom

參考資料

windows下使用vscode編寫運行以及調試C/C++
C/C++ for Visual Studio Code (Preview)

使用Visual Studio Code開發(編譯、調試)Java程序

輕量級的開發平台Visual Studio Code,其可以方便的與git相結合。並且最近推出了Visual Studio Code Installer for Java。大家也可嘗試使用。具體中文說明可以參考這篇文章微軟為 Java 開發者推出 VSCode 安裝程序

總的來說比寫C++程序容易很多。大家可以嘗試一下。


免責聲明!

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



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