安裝C/C++插件
打開插件頁面,搜索輸入C/C++搜索C/C++插件。
安裝該插件后,使用vscode打開包含cpp文件的文件夾時(shí),vscode會添加.vscode子文件夾到目錄中。
添加c_cpp_properties.json配置
通過快捷鍵??P運(yùn)行C/Cpp: Edit configurations,添加缺失的c_cpp_properties.json文件。默認(rèn)的添加的文件如下:
{ "configurations": [ { "name": "Mac", "includePath": [ "${workspaceFolder}/**" ], "defines": [], "macFrameworkPath": [ "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks" ], "compilerPath": "/usr/bin/clang", "cStandard": "c11", "cppStandard": "c++17", "intelliSenseMode": "clang-x64" } ], "version": 4 }
這個(gè)部分沒有做什么調(diào)整。采用的是默認(rèn)添加的文件。
添加tasks.json配置文件
通過快捷鍵??P選擇執(zhí)行的命令,選擇Task: Configure Task命令,選擇Create tasks.json from templates,選擇Others來創(chuàng)建一個(gè)外部命令。根據(jù)自己的編譯器更換commnd選項(xiàng)。
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "cpp", "type": "shell", "command": "g++", "args": [ "-g", "${file}", "-o", "a.out" ], "group": { "kind": "build", "isDefault": true } } ] }
這里需要說一點(diǎn),希望對當(dāng)前標(biāo)簽頁的代碼進(jìn)行編譯執(zhí)行,因此args參數(shù)中用的是${file}。另外需要說的一點(diǎn)是,如果不指定輸出的編譯文件,會影響調(diào)試。
>> g++ -g question.cpp -o a.out
當(dāng)然也可以把a(bǔ)rgs中的a.out替換為${file}保持和文件名的對應(yīng)。
添加launch.json配置文件
在調(diào)試界面點(diǎn)擊運(yùn)行,會提示添加launch.json配置文件。其定義了啟動(dòng)調(diào)試文件的相關(guān)屬性。
{ // 使用 IntelliSense 了解相關(guān)屬性。 // 懸停以查看現(xiàn)有屬性的描述。 // 欲了解