乌市泽哥 发表于 2024-6-29 07:39:58

【小白向】MAC端VSCode C++情况设置(超干货、超详细)

提示:使用情况为 MAC(M2)
其实 VSCode 很早就下载好了,但是由于在设置过程中总是遇到许多坑,搁置了很久,回头捡起遇到报 Error 照旧两眼抓瞎,到处翻 blog。为了淘汰以后的遇坑大概性,整理了这份笔记(支持编译多cpp文件,支持C++11以上的新特性),希望能够资助小白同学避坑。
分两个版本,本文是详细版本。
   版本区别如下:

[*]对于偶然间的朋友:请检察详细版本。本着“授人以鱼不如授人以渔”的想法,和大多数的博客不同,此版本里除了详细记载了设置步调,也基于官方文档拓展了设置文件所对应的功能和参数简介,能够让大家举一反三,后续碰到问题时快速做定位。
[*]对于没耐烦希望快速解决当前问题的朋友:跳转 TLDR(too long, dont read)版本。只包罗教程步调。
详细版本

条件


[*]安装 Visual Studio Code
[*]VSCode 中安装 C/C++扩展
https://img-blog.csdnimg.cn/direct/6b088d68c247459c92627a972ad52a67.png#pic_center#
[*]确保 Clang 已经安装(在终端中输入命令:clang --version 来确认是否安装)
创建你的第一个 HelloWorld 程序

创建工作区(WorkSpace)

打开 VSCode, 创建一个 project 文件夹。
https://img-blog.csdnimg.cn/direct/ef94613cbccc4f92b3f93a2624b9acbd.png#pic_center#
现在,project 就是我们的工作区(WorkSpace)了。当我们继承做完本教程的设置,这个工作区中将出现一个子文件夹.vscode,包罗三个文件。
● tasks.json(编译选项设置)
● launch.json(调试选项设置)
● c_cpp_properties.json(编译器路径以及IntelliSense 设置)
创建 HelloWorld 的源代码文件

在 project 文件夹下创建一个 hello.cpp 文件
https://img-blog.csdnimg.cn/direct/53867114a1fb4c98bd7fbce59b085ff8.png#pic_center#
在 hello.cpp中粘贴以下内容
#include <iostream>

using namespace std;

int main(){
    cout << "Hello World" << endl;
}
Command+S(⌘S)保存该文件,建议在 File > Auto Save 中打开该选项,自动保存。
https://img-blog.csdnimg.cn/direct/8b352d0ef9d3476db66fc311bb6dbfcd.png#pic_center#
添加 IntelliSense 设置

IntelliSense 是 VS Code 中提供的工具,可实当代码补全等,资助快速开发。
Shift+Command+P(⇧⌘P)打开命令面板,输入Select IntelliSense Configuration,选择 Use clang++
可以看到工作区中新增了一个.vscode 的文件夹,并包罗了一个叫做 settings.json 的文件
https://img-blog.csdnimg.cn/direct/2585aec1bee444e284184498ef5f0ab3.png#pic_center#
运行 HelloWorld(compile and run)

注意:C++扩展是使用呆板上已安装的 C++编译器来天生程序,以是在运行/调试 hello.cpp 前,请确保你已经符合了文章开头的条件,安装好了 C++编译器。

[*]打开 hello.cpp,这将它将成为 active file,可以被用于天生和调试
[*]点击图中右上角的按钮,选择 Run C/C++ File
https://img-blog.csdnimg.cn/direct/c87639063c4c4924835c03c7e34d3f31.png#pic_center#
[*]在跳出的选项中,选择C/C++: clang++ build and debug active file。
https://img-blog.csdnimg.cn/direct/c530774b7fef473ab89c7cfd72d17635.png#pic_center#
只有在第一次运行 hello.cpp 时才必要做这个选择。选择后,.vscode 文件夹中出现了一个新文件tasks.json,我们选择的编译器设置将作为默认设置。
[*]编译成功后,程序的输出将表现在下方的 DEBUG CONSOLE
https://img-blog.csdnimg.cn/direct/c36a6aa8df074e56a1064910075ba29e.png#pic_center#
至此,我们已经成功地运行了 VS Code 上的第一个 C++程序!
改造代码目次(include、src、target) 以适应工程必要

在实际的开发中,我们往往必要分类不同的文件至不同的文件夹中。一个简单的形式是在此中创建三个子文件夹 include、src、target 分别用来分类头文件、源文件、以及目标程序。
在 project 文件夹中,清算之宿世成出来的其他文件(除了 hello.cpp 和.vscode),增长三个子文件夹 include、src、target,并把 hello.cpp 移动到 src 目次下。
https://img-blog.csdnimg.cn/direct/c88537669ac84feba167e2c1fc53f144.png#pic_center#
修改 tasks.json

修改了文件组织形式后,存储了编译设置的 tasks.json 也必要做对应修改。
第一次编译运行 hello.cpp 时,我的呆板上天生的 tasks.json 如下:
{
    "tasks": [
      {
            "type": "cppbuild",
            "label": "C/C++: clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-fcolor-diagnostics",
                "-fansi-escape-codes",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
      }
    ],
    "version": "2.0.0"
}
我们做如下两个修改:

[*]工程中往往必要一次性编译选定路径下的全部 cpp 文件。对应的,把"args"中的:"${file}"改为"${fileDirname}/*.cpp"
[*]我们希望把编译出的文件统一放置在 target 文件夹下。对应的,把"args"中的"${fileDirname}/${fileBasenameNoExtension}"改为"${workspaceFolder}/target/${fileBasenameNoExtension}"
https://img-blog.csdnimg.cn/direct/a3569f002ae14e7cb3044f0f3ede0de6.png#pic_center#
然后我们做下测试,看看修改后的工作区可否正常编译运行 hello.cpp
在 include 中创建 print.h,粘贴如下内容:
#include <iostream>
#include <vector>
#include <string>
using namespace std;

void printMessage(vector<string>& msg);
在 src 中创建 print.cpp,粘贴如下内容:
#include "../include/print.h"

void printMessage(vector<string> &msg)
{
    for (int i = 0; i < msg.size(); i ++){
      cout << msg << " ";
    }
}
修改 src 下的 hello.cpp,粘贴如下内容:
#include "../include/print.h"

int main(){
    vector<string> msg;
    msg.push_back("Hello"); msg.push_back("World");
    printMessage(msg);
}
在 hello.cpp 中点击右上角的 run 按钮,可以看到也成功运行了(这说明我们实现了一次性编译多个 cpp 文件),而且目标文件天生到了 target 下。
https://img-blog.csdnimg.cn/direct/484b510d65c146a29213061e7ca2b03c.png#pic_center#
拓展:关于 tasks.json 的设置寄义

tasks.json 中存储了编译的设置,更深刻地理解它有助于解决后续大概遇到的编译错误。
以上面贴出的 task.json 内容为例,对 tasks.json 中的参数解析如下:


[*]label
*第一次点击 Run C/C++ File 时指定的选项值。在本例中,是"C/C++: clang++ build active file"
[*]command
指定要运行的程序(编译器) 在本例中,是 clang++
[*]args
指定通报给编译器的命令行参数,这些参数应当按照一定的顺序分列       在最开始天生的 tasks.json 中,args 相关参数设置如下:
"args": ["-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"]

他的寄义是告知 C++编译器编译 active file(即${file}),天生目标文件(-o)到当前的目次下
(${fileDirname}) ,目标文件的名称和不带后缀的 active file 的名称同等(${fileBasenameNoExtension})。
因此我们第一次运行时,在 hello.cpp 中 run,在 同级目次下编译出了 hello 目标文件。

至此,应该也能理解将原先的"-g", "${file}"改为"-g", "${fileDirname}/*.cpp"带来的影响了吧,就是将编译当前 active file 酿成编译同级目次下的全部 cpp 文件

而将"-o", "${fileDirname}/${fileBasenameNoExtension}"
改为"-o", "${workspaceFolder}/target/${fileBasenameNoExtension}"则是改变目标文件的路径到工作区下的 target 目次下。
[*]problemMatcher:
在编译器输出中查找错误和警告的输出解析器。对于 clang++,gcc 是最佳的 problemMatcher
[*]group:
里面的 isDefault 选项可以用来指定哪个 task 被作为默认的天生选项。天生了 tasks.json 后,点击 run 按钮就会按照默认选定的 task 设置进行编译运行,tasks 中可以有多个 task,不必要作为默认项的移除
“group"中的"isDefault”: true
[*]detail:
task 的描述。tasks 中可以用多个 task,detail 可用于做区分
调试 HelloWorld(debug)



[*]起首,回到 hello.cpp,使得其变为 active file
[*]在如图所示处设置一个断点(在该行的前面点击小圆点)
https://img-blog.csdnimg.cn/direct/ea2a396d9eff4e2fb50e116ac118d47d.png#pic_center#
[*]选择 Debug C/C++ File
https://img-blog.csdnimg.cn/direct/3c06b383549c4398a79d43f680da67e6.png#pic_center#
由于之前我们已经在 hello.cpp 上选择过 Run C/C++ File 编译运行过了,其时选择了C/C++: clang++ build and debug active file,此时就不必再选择了。如果此前没有运行过就选择了 debug,那么在跳出的选项中选择C/C++: clang++ build and debug active file。
https://img-blog.csdnimg.cn/direct/e4a6544470ce496499cc0362311b369b.png#pic_center#
[*]可以看到程序执行到端点处,执行位置有高亮表现。出现了调试的工具栏,可以分别选择执行到下一个断点、执行单步、步入、跳出、重新开始、制止。这说明可以正常调试了。
https://img-blog.csdnimg.cn/direct/240ed67fc99148c5a11d52383110d029.png#pic_center#
设置调试设置 launch.json

当我们点击调试按钮(Debug C/C++ File)时,其实内部是为我们创建了一个动态调试设置,但往往我们必要去对调试设置做一些修改,可以创建一个 launch.json 文件来存储我们的调试设置。


[*]点击右上角的 Add Debug Configuration 按钮
https://img-blog.csdnimg.cn/direct/f9e0fed223864ac6bbd3a4e50fb9d991.png#pic_center#
[*]在跳出的选项里选择C/C++: clang++ build and debug active file.
https://img-blog.csdnimg.cn/direct/21de7ae7e7b943ec9434e0614cb9526e.png#pic_center#
[*].vscode 中会自动创建一个 launch.json 文件
{
    "configurations": [
      {
            "name": "C/C++: clang++ build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/target/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "preLaunchTask": "C/C++: clang++ build active file"
      }
    ],
    "version": "2.0.0"
}
此中,重要的几个参数释义如下:


[*]program:必要调试的程序。如果我们是在 hello.cpp 文件中开启调试,则对应调试的程序是project/target/hello
[*]args:运行时通报给程序的参数数组
[*]stopAtEntry:为 true 时,程序将在main 方法入口处添加断点
[*]preLaunchTask:此处的值应当和tasks.json 中的 label 值保持同等
当我们创建了 launch.json,以后在这个程序上的调试都将按照 launch.json 中的调试设置开展。
设置c_cpp_properties.json

可以通过创建c_cpp_properties.json 文件来实现对 C/C++扩展 的更多设置,这里的正确设置可以资助扩展找到正确的标准库路径,提供补全或跳转本事,如果程序包罗不在工作区或标准库路径中的头文件,需修改此中的“includePath”设置。
Shift+Command+P(⇧⌘P) 打开命令面板,输入C/C++: Edit Configurations (UI),打开。这将在.vscode 中创建一个c_cpp_properties.json 文件。
https://img-blog.csdnimg.cn/direct/39e584160bef4180ae584c0a5ca234b7.png#pic_center#
在界面中做如下修改:
https://img-blog.csdnimg.cn/direct/96463caf9fcb4067837861c896c9e0ba.png#pic_center#
https://img-blog.csdnimg.cn/direct/6eaa05bd441643989b3fa2ebc29dc708.png#pic_center#
也可以在c_cpp_properties.json 中直接修改
https://img-blog.csdnimg.cn/direct/ce27cc30ef1843d096a22089a04830be.png#pic_center#
我的c_cpp_properties.json 如下,可以直接复制修改:"cStandard": "c17", "cppStandard": "c++17","compilerPath": "/usr/bin/clang++"这三处。
{
    "configurations": [
      {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ],
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "macos-clang-arm64",
            "compilerPath": "/usr/bin/clang++"
      }
    ],
    "version": 4
}
解决关于 C++新特性支持的问题

在前面的示例中,我故意地在代码里避开了 C++的新特性。
如果我们将代码修改成如许
print.cpp:
#include "../include/print.h"

void printMessage(vector<string> &msg)
{
    for (const auto& word: msg){
      cout << word << " ";
    }
}
hello.cpp:
#include "../include/print.h"

int main(){
    vector<string> msg{"Hello", "World"};
    printMessage(msg);
}
运行会发现,将报告如下的错误,不丢脸出是 C++11 的新特性没有被支持。
https://img-blog.csdnimg.cn/direct/74568aa97e1f4c66aee2da325a6fb521.png#pic_center#
这种编译报错怎么处理?
还记得我们前面说到的编译选项在那里设置吗?yes,tasks.json!
在 tasks.json 文件的 args 中添加"–std=c++17"选项,使编译支持 C++新特性
"args": [
                "-fcolor-diagnostics",
                "-fansi-escape-codes",
                "--std=c++17",
                "-g",
                "${fileDirname}/*.cpp",
                "-o",
                "${workspaceFolder}/target/${fileBasenameNoExtension}"
            ],
成功运行。
https://img-blog.csdnimg.cn/direct/102445d1c84942739b72aa882e0a062e.png#pic_center#
成功调试。
https://img-blog.csdnimg.cn/direct/9c637b7796f54ab9b941803e96e3386d.png#pic_center#

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 【小白向】MAC端VSCode C++情况设置(超干货、超详细)