下载C/C++插件
方法一:通过配置编译文件,将代码编译成可执行文件然后运行
1、在文件根目录下创建.vscode文件夹
2、在.vscode文件夹下创建3个json配置文件分别命名为:launch.json、tasks.json、tasks.json
launch.json(C++调试的配置文件)
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug C++ file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build",
"miDebuggerPath": "/usr/bin/lldb",
"logging": {
"engineLogging": true
},
"visualizerFile": "${workspaceFolder}/gdb-visualizers.xml",
"internalConsoleOptions": "openOnSessionStart",
"showDisplayString": true
}
]
}
task.json(C++编译的配置文件)
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "build",
"command": "/usr/bin/clang++",
"args": [
"-std=c++11",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": ["$gcc"]
}
]
}
c_cpp_properties.json (这个配置文件是 VS Code 的 C/C++ 扩展插件的 c_cpp_properties.json 文件,主要用于配置项目的 IntelliSense、编译器路径、C/C++ 标准等)
{
"version": 4,
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "macos-clang-arm64",
"compilerPath": "/usr/bin/clang++"
}
]
}
3、在项目根目录下创建cpp源文件(一下为一个示例)
main.cpp
#include<iostream>
using namespace std;
int main()
{
cout << "hello world" << endl;
return 0;
}
按住cmd+shift+B选择编译文件(一般为第一个),编译完成后按任意键结束,重新打开终端,输入:
./main
即可运行成功。
注:这里的main是你源文件的名字
编译会生成一些文件,不需要在意
方法二:通过插件code runner运行
以上配置文件不要动
下载这个插件
右键点击run code或者点击右上角三角形:
ps:code runner插件默认支持的是C++98,对于C++11新特性不支持,需要手动更改插件设置。