由于Visual Studio for Mac 不支持C++,所以在此记录下使用VScode运行调试C++程序的步骤。
首先打开存放程序的文件夹
然后新建一个cpp文件,内容如下:
然后需要新建两个配置文件,launch.json和tasks.json。
1 首先在左侧的运行栏那边,创建launch.json文件
依次如下图选择
然后把文件内容改为:
{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
//"program": "${workspaceFolder}/test.out",
//上一行是官方写法,但是不同的cpp调试都要改配置,非常麻烦
"program": "${workspaceFolder}/${fileBasenameNoExtension}",
"args": [],
"cwd": "${workspaceFolder}",
"preLaunchTask": "Build with Clang"
}
]
}
2 shift+command+b 创建tasks.json文件
然后把文件内容改为:
{
"version": "2.0.0",
"tasks": [
{
"label": "Build with Clang",//这个任务的名字在launch.json最后一项配置
"type": "shell",
"command": "clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
//"test.cpp",这里是官方写法,不具有普遍性,注意两个配置文件的统一性即可
"${fileBasenameNoExtension}.cpp",
"-o",
//"test.out",
"${fileBasenameNoExtension}",
"--debug"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
接着就可以开始编译并调试了。
选中hello.cpp文件,然后shift+command+b 编译生成hello文件
然后设一个断点,到调试页面去运行调试。
可以看到,成功调试。