Mac下vscode配置cpp环境
1.安装vscode
2.插件安装
安装“C/C++”、“C/C++ Clang Command Adapter”
3.新建cpp文件
4.配置1 c_cpp_properties.json
按 Command + Shift + P 打开命令行面板,输入 C/Cpp: Edit Configurations命令
此时会在当前工作空间目录生成.vscode配置目录
同时在配置目录会生成一个c_cpp_properties.json文件
将要用到的库的路径添加到includePath数组中
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}",
"/Library/Developer/CommandLineTools/usr/include/c++/v1",
"/usr/local/include",
"/Library/Developer/CommandLineTools/usr/include",
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++98",
"intelliSenseMode": "macos-clang-x64"
}
],
"version": 4
}
5.配置2 tasks.json
按 Command + Shift + P 打开命令行面板,输入 tasks,选择 Tasks:Configure Task 生成 tasks.json 配置文件,选择的模板为MSBuild
该文件其实就是一个命令行构建工具
把运行程序时在终端输入的命令和参数对于"command"和"args"的值
输入shift+command+b,便可构建成功,生成可执行文件a.out
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "c++",
"command": "clang++",
"type": "shell",
"args": [
"main.cpp",
"-std=c++11",
"-g"
],
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "new"
}
}
]
}
6.配置3 launch.json
如果需要对项目进行进行 debug,则需要先生成 launch.json 文件并对其做相应的配置
-
按
Command + Shift + D进入到运行界面 -
点击
创建 launch.json 文件,选择C++(GDB/LLDB)
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "c/c++ Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb",
"preLaunchTask":"c++"
}
]
}

7.shift+command+b编译失败
于VScode报错“终端将被任务重用,按任意键关闭”的解决方案:
原因是本地端口是被共享的,所以必须关闭一个进程才能执行另一个,只需要对 tasks.json文件做一些修改即可。
tasks.json里把"panel"="shared"改成:"panel"="new"
8.如何运行?
输入shift+command+b,便可构建成功,生成可执行文件a.out
编译完成后,会在项目路径下生成 main 文件,使用 ./main 即可执行
出现错误,重新参考,理解三个配置文件
在配置环境之前先检查一下机器上都安装了什么C++编译器,通过以下两条命令可以查看是否安装了g++/clang编译器
g++ --version
clang++ --version
然后安装两个插件
然后新建工程目录,在里面新建一个cpp文件命名为main.cpp。
然后点击侧边栏的Debug按钮,点击设置图标,便会提示你选择环境,这里就选择C++那一项。
此时VSCode会在你的工程目录下自动新建一个.vscode的文件夹,并新建了一个launch.json的文件,这里需要对生成的文件进行一些小改动。本人配置如下:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out",
//a.out 是需要运行的二进制文件可以改名
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb"
}
]
}
保存后按快捷键⇧⌘B编译,此时会出现提示没有找到要运行的生成任务,所以接下来将进行生成任务的配置工作,VSCode提供了一些模版,有需要的可以自行选择,这里就选则Others。
此时.vscode目录下会出现一个task.json文件,对它进行改写。本人配置如下:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "hello world", //描述
"type": "shell",
"command": "clang++",
"args": [
"main.cpp" //文件名
],
"group": {
"kind":"build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "new" //新建端口
}
}
]
}
保存后再次按快捷键⇧⌘B就能够顺利编译了,编译完成后按F5执行,得到输出结果。
每次编译完成后,我们会发现目录下多了一个a.out文件,这个文件是Linux/Unix环境下编译器编译源代码并连接产生的可执行文件,在未指定的情况下其默认命名为a.out。那么如何通过修改配置文件来修改这个文件的命名呢?
方法很简单,在task.json中的args属性下填入-o yourfilename.out
可以发现这里的command和args两个属性就相当于在命令行中执行了clang++ -o yourfilename.out main.cpp,所以如果还有其他的需求也可以对这里进行改写。最后不要忘记修改launch.json中的program属性,将.out的文件名修改为与task.json一致。便可以成功编译执行了。
用了第二种之后,control+command+b 编译
再F5执行,不行,应该是branch文件配置的原因。
但是./\app.out 可以运行。先这样。
参考:
https://blog.csdn.net/qq_43119354/article/details/89219180?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_title~default-0.control&spm=1001.2101.3001.4242

浙公网安备 33010602011771号