MAC Pro 安装 VS Code 配置 C/C++ 开发环境
创始人
2024-05-25 05:44:59
0

目录

文章目录

  • 目录
  • 安装 VS Code
  • 配置 C/C++ 开发环境
  • Hello World
    • 1、创建项目和源码
    • 2、编译运行
    • 3、调试
  • C/C++ configuration

安装 VS Code

  1. 下载安装包:https://code.visualstudio.com/Download
  2. 解压并将文件放入 “应用程序"。

在这里插入图片描述

配置 C/C++ 开发环境

官方文档:https://code.visualstudio.com/docs/cpp/config-clang-mac

  1. 安装 C/C++ 编程常用扩展插件:
    1. C\C++:language support
    2. C\C++ Clang Command Adapter:Clang command
    3. C/C++ Extension UI Themes
    4. CMake
    5. CMake Tools
    6. Makefile Tools
    7. Code Runner:用于编译。
    8. CodeLLDB:用于 Debug,解决 Catalina 不支持 LLDB 调试问题。

在这里插入图片描述

  1. 检查 MacOS 上的 Clang/LLVM 环境。
$ clang --version
Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: x86_64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
  1. 安装 code 指令,便于通过指令行进行操作。
    在这里插入图片描述

Hello World

1、创建项目和源码

mkdir /workspace/projects
cd /workspace/projectsmkdir helloworld
cd helloworld
code .

使用 code 指令将该目录定义为一个代码项目的工作目录,在该目录下我们会创建 1 个 VS code 代码项目专有的配置目录 .vscode,包含了以下 3 个文件:

  1. tasks.json (compiler build settings)
  2. launch.json (debugger settings)
  3. c_cpp_properties.json (compiler path and IntelliSense settings)

创建 C/C++ 源码文件:

#include int main()
{for (int i=0; i<3; i++){std::cout << "hello world" << std::endl;}return 0;
}

2、编译运行

  • 运行并查看输出:
    在这里插入图片描述

  • 第一次运行后会自动生成 tasks.json 文件,设置了代码编译和构建的配置参数。

{"tasks": [{"type": "cppbuild","label": "C/C++: clang++ 生成活动文件","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": "调试器生成的任务。"}],"version": "2.0.0"
}
  • 对应的编译指令:
/usr/bin/clang++ -fcolor-diagnostics -fansi-escape-codes -g /workspace/projects/helloworld/helloworld.cpp -o /workspace/projects/helloworld/helloworld

3、调试

  • 单击行号插入 breakpoint。
    在这里插入图片描述

  • 进入调试界面开始调试:
    在这里插入图片描述

在需要自定义 debug 流程的场景中,可以通过添加 launch.json 配置文件来完成。

  • 原始文件:
{"configurations": [{"name": "C/C++: clang++ 生成和调试活动文件","type": "cppdbg","request": "launch","program": "${fileDirname}/${fileBasenameNoExtension}","args": [],"stopAtEntry": false,"cwd": "${fileDirname}","environment": [],"externalConsole": false,"MIMode": "lldb","preLaunchTask": "C/C++: clang++ 生成活动文件"}],"version": "2.0.0"
}
  • 自定义使用 CodeLLDB 插件:
{// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0","configurations": [{"type": "lldb","request": "launch","name": "Debug","program": "${fileDirname}/${fileBasenameNoExtension}.out","args": [],"cwd": "${workspaceFolder}"}]
}

C/C++ configuration

最后,针对 C/C++ 的诸多扩展内容,我们可以通过 UI 的方式来设置相关的配置,同时会自动生成 c_cpp_properties.json 文件。

在这里插入图片描述

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...