個人博客 chinazt.cc
閑話少敘,直奔主題
- 下載VSCode
https://code.visualstudio.com/download
- 安裝C/C++插件
需要兩個插件:
1. cpptools
這個插件用來查找頭文件和源代碼,用於代碼提示。
2.clang++
這個插件用來自動編譯源代碼,如果你習慣使用makefile獲取其它構建工具,那么可以不安裝這個插件。
- 配置插件
1.進入命令行模式, 選擇[C/Cpp: Edit Configurations]。生成c_cpp_properties.json,在對應的MAC節點中,根據實際需要修改頭文件所在路徑。
2.進入命令行模式, 選擇[Tasks: Configure Task Runner]。生成tasks.json,里面內容如下:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "clang", //使用clang編譯C文件,如果你使用C++開發,改成clang++
"isShellCommand": true,
"args": ["main.c", "-g"],//如果使用的是C++,則改成main.cpp或者相對應的cpp入口文件。如果需要支持C++11,添加"-std=c++11"
"showOutput": "always"
}
3.進入命令行模式, 選擇[Debug: Open launch.json]。生成launch.json文件,內容如下:
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch (GDB)",
"type": "cppdbg",
"request": "launch",
"targetArchitecture": "x86_64",
"program": "${workspaceRoot}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb",
"preLaunchTask": "clang"
}
]
}
- 測試插件
#include<stdio.h>
int
main()
{
printf("Hello World");
}
使用Shift+Command+B預期會自動編譯出a.out。 執行F5預期可以自動進行debug。