c++ vscode 自动注释_WIN下vscode调试C/C++,从零开始生成helloworld项目【2018年6月29日】...
最近迷上了vscode這款編譯器,小巧美觀,用起來也很順手,最主要的是全平臺,正好最近要上手做Linux C客戶端,以前沒接觸過linux,先拿它先在WIN上練練手。
這幾天在網上找了非常多的教程,win總是配不好環境。(linux和win一起開始配的,感覺linux環境比win好配多了,下一篇博文再把linux端的配置過程分享出來,這里先寫win的)。
環境:
WIN10 64?專業版
vscode版本:1.24.1
launch.json版本:0.2.0
tasks.json版本:2.0.0
mingw-w64版本:8.1.0
過程:
一、 安裝vscode
vscode官網下載安裝包直接安裝即可
二、 vscode內安裝C/C++?插件
vscode內按快捷組合鍵Ctrl+Shift+X(或如圖第①步點擊[拓展]按鈕)打開拓展分頁,在搜索欄輸入" C ",查找到如圖的第一個插件,安裝并重新加載之。
三、 安裝mingw-w64(具體安裝與環境變量配置可以查看這里)
在mingw-w64官網下載64位的mingw-w64在線安裝包(以在線包為例)或離線包(離線包直接解壓出來就能用)
在線包:根據系統選擇合適的安裝包進行下載,選擇在線安裝器
下載完成后出現如下安裝包
安裝該包,在Setting?界面將Architecture選項改為x86_64,其他不變,選擇合適的安裝路徑(默認或重新指定都可以,路徑中不要有中文)
配置計算機環境變量如圖(我的安裝路徑是D:\mingw64,因此環境變量這么加)
安裝完成后打開控制臺,分別輸入? ?g++ --version? ?和 gcc --version? 查看環境是否安裝成功(是否有當前版本號)
四、重啟電腦(這里看了其他很多博主的沒有提到,我沒有重啟,后來vscode代碼寫出來跑了很多次提示沒有找到g++命令,最后重啟解決)
五、運行C++代碼
打開vscode,選擇或新建一個空文件夾目錄打開作為項目目錄,新建一個test.cpp文件,鍵入如下helloworld代碼
#include
int main(int argc, char *args[])
{
int i, j;
printf("hello world!\n");
printf("argc:%d\nargv:\n", argc);
for (i = 0; i < argc; i++)
{
printf("%d:%s\n", i, args[i]);
}
getchar();
return 0;
}
按下F5,頂部或出現如下菜單,選擇C++(GDB/LLDB)
系統自動在當前目錄下創建.vscode文件夾,并在其中新建一個launch.json的模版文件如下:
{
// 使用 IntelliSense 了解相關屬性。
// 懸停以查看現有屬性的描述。
// 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "enter program name, for example ${workspaceFolder}/a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "/path/to/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
將該模版修改為如下(可以直接復制,并修改有注釋的一段)
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"preLaunchTask": "build",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:/mingw64/bin/gdb.exe", // 這里修改GDB路徑為安裝的mingw64的bin下的gdb.exe路徑
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}]
}
vscode中按下組合鍵Shift+Ctrl+P,在喚出的任務欄中鍵入>task,下拉找到并點擊 Tasks:Configure Task(任務:配置任務)項,并在接下來的返回項中選擇使用模版創建tasks.json文件
系統會自動在.vscode文件夾下創建一個tasks.json文件,自動生成的代碼如下
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "msbuild",
"args": [
// Ask msbuild to generate full paths for file names.
"/property:GenerateFullPaths=true",
"/t:build"
],
"group": "build",
"presentation": {
// Reveal the output only if unrecognized errors occur.
"reveal": "silent"
},
// Use the standard MS compiler pattern to detect errors, warnings and infos
"problemMatcher": "$msCompile"
}
]
}
同理,將之修改為如下代碼(可直接覆蓋)
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
},
"windows": {
"command": "g++",
"args": [
"-ggdb",
"\"${file}\"",
"--std=c++11",
"-o",
"\"${fileDirname}\\${fileBasenameNoExtension}.exe\""
]
}
}
]
}
至此,環境配置完成,轉到C++代碼頁,按下F5,根目錄下出現.cpp文件同名的.exe文件,代碼自動執行,完成。
? ? ? ??
六、運行C代碼
仿照第五步,新建helloworld.c文件,鍵入或粘貼C語言的helloworld代碼
#include
#include
int main() {
printf("hello world!\n");
system("pause");
return 0;
}
在.c頁面內單擊F5,稍候片刻出現同名.exe并自動執行,完成。
最后,感謝以下博主的博客進行參考
總結
以上是生活随笔為你收集整理的c++ vscode 自动注释_WIN下vscode调试C/C++,从零开始生成helloworld项目【2018年6月29日】...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java程序启动后就进行了7次young
- 下一篇: c++ hough变换代码_hough变