Bootstrap

MAC VSCODE配置C语言开发环境

MAC VSCODE配置C语言开发环境

文章系转载,方便整理和归纳,源文地址 https://www.cnblogs.com/Steward-Sun/p/12247475.html

  1. 安装VS Code

  2. 打开VSCode 按快捷键shift+command+X,并在搜索框输入c/c++, 安装C/C++组件

  3. 在Finder中创建空文件夹并在VS Code中打开, 并File => Save workspace As 将工作区间另存为

  4. 新建Hello.c文件并保存

  5. 接下来需要配置3个JSON文件c_cpp_properties.json、tasks.json、launch.json

  6. c_cpp_properties.json : 使用快捷键command+shift+p /F1 打开命令行面板(Command Palette)

    输入edit configurations,在弹出的列表中选择带JSON的C/C++:Edit Configurations(JSON)

    此时会自动新增.vscode文件夹,并新建c_cpp_properties.json文件

配置字段 includePath

"includePath": 
[
    "${workspaceFolder}/**",
    "/Library/Developer/CommandLineTools/usr/include/c++/v1",
    "/usr/local/include",
    "/Library/Developer/CommandLineTools/usr/lib/clang/10.0.1/include",
    "/Library/Developer/CommandLineTools/usr/include"
],

你可能需要注意
"/Library/Developer/CommandLineTools/usr/lib/clang/10.0.1/include",中的版本号,可以进入Finder,按快捷键command+shift+G输入

/Library/Developer/CommandLineTools/usr/lib/clang/   以此来查看版本号
  1. tasks.json : 在打开.c文件的情况下(比如我这里的hello.c)打开命令行面板command+shift+P,输入tasks:configure task,选择Tasks:Configure Task

    点击C/C++:gcc build active file

​ 自动生成tasks.json文件并打开

这里我们需要配置args字段

"args": [
    "-g",//编译命令
    "${workspaceFolder}/hello.c",//当前workspace下的hello.c文件
    "-o",//输出文件命令
    "${workspaceFolder}/hello.o"//生成的可执行文件的路径及名称
    ],
  1. launch.json :
  • 打开命令行面板command+shift+P,输入launch,选择Open launch.json
  • 选择环境为C++(GDB/LLDB)
  • 自动生成launch.json文件并打开

配置 program字段

这个字段是要运行的文件路径,写你生成的可执行文件的路径即可,比如我这里是

 "program": "${workspaceFolder}/hello.o",
  1. Shift + Command + B 构建

  2. F5 调试

扩展vscode命令工具: https://code.visualstudio.com/shortcuts/keyboard-shortcuts-macos.pdf
vscode c_cpp_properties配置说明文档:https://code.visualstudio.com/docs/cpp/c-cpp-properties-schema-reference

;