1.系统:Mac OS Sonoma 14.2
2.vs code 1.85
3.Xcode
1.chinese 汉化
2.c/c++
3.Code Runner
在终端输入
clang --version
如果没有安装,安装Xcode
xcode-select --install
1.选择磁盘,创建一个专用的文件夹,从VScode中打开文件夹
2.在文件夹下创建cpp文件
#include
#include
#include
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
1.保存文件
2.点击运行c/c++文件,选择如图所示的。如果未弹出,按F5,然后如下图所示。会自动生成tasks.json文件
选择第一个
选择第一个c++(gdb/lldb)
选择第二个,clang++
编译成功后,会成功输出
配置tasks.json文件,以下是我的配置文件
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++ 生成活动文件",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-fcolor-diagnostics",
"-fansi-escape-codes",
"-g",
"${file}",
// "${workspaceFolder}/*.cpp" // 多cpp文件编译
"-o",
// "${fileDirname}/${fileBasenameNoExtension}" // 生成文件路径
"${workspaceFolder}/build/${fileBasenameNoExtension}"
// "-fcolor-diagnostics",
// "-fansi-escape-codes",
// "-g",
// "${file}",
// "-o",
// "${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "C/C++: clang++ build and debug active file",
"type": "cppdbg",
"request": "launch",
// 可执行文件,和task.json中的-o对应的可执行文件输出路径一致
"program": "${workspaceFolder}/build/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
// "externalConsole": true,
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "C/C++: clang++ 生成活动文件"
}
]
}
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
// "/Library/Developer/CommandLineTools/usr/include",
// "/Library/Developer/CommandLineTools/usr/lib/clang/15.0.0/include",
// // 请根据你的clang版本修改,我这里是11.0.3
// "/usr/local/include"
],
"defines": [],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
// "/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang++",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "macos-clang-arm64"
}
],
"version": 4
}
运行代码,正常输出
设置断点,进行debug调试
如果需要进行cin操作,vscode内的终端无法输入,目前的解决办法是将launch.json文件中的将"externalConsole": false,中的false改为true。即可调用外部终端