首頁 > 軟體

VsCode設定C++/Cmake的步驟詳解

2021-07-25 16:00:02

Reference

https://zhuanlan.zhihu.com/p/87864677

步驟

1、安裝VSCode,直接在官網下載 安裝即可
2、設定C/C++環境,安裝MinGW編譯器,也可以在 官網 下載安裝
3、MinGW編譯器剛才下載的是個下載器,直接雙擊安裝,設定X86和WIN32,指定下載目錄(需要記住,之後會用,並且目錄不能有空格)
4、下載完成之後,將剛才下載目錄下的bin資料夾目錄設定到環境變數裡


5、CMD視窗輸入gcc -v不報錯就證明設定成功

6、VSCode中搜尋C/C++擴充套件進行安裝

7、開始設定C/C++環境:

 (1).設定編譯器

VSCode中 Ctrl+Shift+P調出命令面板,輸入C/C++,選擇「Edit Configurations(UI)」進入設定。設定一,找到編譯器路徑:設定你剛才的安裝路徑下的g++.exe,例如 D:/mingw-w64/bin/g++.exe。 設定二,找到IntelliSense 模式:gcc-x64;
設定完成後,此時在側邊欄可以發現多了一個.vscode資料夾,並且裡面有一個c_cpp_properties.json檔案,內容如下,說明上述設定成功。現在可以通過Ctrl+<`快捷鍵開啟內建終端並進行編譯執行了。


{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.18362.0",
            "compilerPath": "C:/Program Files/JetBrains/mingw64/bin/g++.exe",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}

(2).設定構建任務

接下來,建立一個tasks.json檔案來告訴VS Code如何構建(編譯)程式。該任務將呼叫g++編譯器基於原始碼建立可執行檔案。 按快捷鍵Ctrl+Shift+P調出命令面板,輸入tasks,選擇「Tasks:Configure Default Build Task」:將task.json內容複製進去,記著更改目錄

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "shell",
			"label": "g++.exe build active file",
			"command": "C:/Program Files/JetBrains/mingw64/bin/g++.exe",
			"args": [
				"-g",
				"${file}",
				"-o",
				"${fileDirname}\${fileBasenameNoExtension}.exe",
				"-std=c++17"
			],
			"options": {
				"cwd": "C:/Program Files/JetBrains/mingw64/bin"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			}
		}
	]
}

(3).設定偵錯設定

這裡主要是為了在.vscode資料夾中產生一個launch.json檔案,用來設定偵錯的相關資訊。點選選單欄的Debug–>Start Debugging:

生成了一個launch.json檔案

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
 
        {
            "name": "(gdb) Launch",
            "preLaunchTask": "g++.exe build active file",
            "type": "cppdbg",//只能為cppdbg
            "request": "launch",
            "program": "${fileDirname}\${fileBasenameNoExtension}.exe",//偵錯程式的路徑名稱
            "args": [],//偵錯傳遞引數
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "internalConsoleOptions": "neverOpen",
            "MIMode": "gdb",
            "miDebuggerPath": "C:/Program Files/JetBrains/mingw64/bin/gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

設定完成,建立個cpp檔案測試一下吧。.vscode資料夾可以先儲存一份,下次可以直接複製到其他資料夾下使用。

VsCode / Cmake 工程

https://www.cnblogs.com/iwiniwin/archive/2020/09/21/13705456.html

https://blog.csdn.net/weixin_43822014/article/details/114500763

用VSCode和CMake編寫偵錯C/C++

到此這篇關於VsCode設定C++/Cmake的文章就介紹到這了,更多相關VsCode設定C++/內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com