mac vscode搭建c语言开发环境

环境

os:macOS Catalina

clang(clang -v 结果):

Apple clang version 11.0.0 (clang-1100.0.33.16)
Target: x86_64-apple-darwin19.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

lldb(lldb -v 结果):

lldb-1100.0.30.11
Apple Swift version 5.1.3 (swiftlang-1100.0.282.1 clang-1100.0.33.15)

vscode插件安装

  1. C/C++

    author:Microsoft

  2. C/C++ Clang Command Adapter

    author:Yasuaki MITANI

  3. CodeLLDB

    author: Vadim Chugunov

    在macOS Catalina这个版本中,取消了对lldb的支持。所以如果需要debug,必须安装这个插件。

task.json

task.json配置C程序构建任务

例如:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "common-build",
            "command": "/usr/bin/clang",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${workspaceFolder}/build/${fileBasenameNoExtension}",
            ],
            "options": {
                "cwd": "/usr/bin"
            }
        }
    ]
}

label选项指定当前任务的代号,这个必须和launch.json中的preLaunchTask选项一致

command选项指定编译的程序,在macOS中使用/usr/bin/clang命令进行编译

args选项指定command命令执行时传递的参数,$ {xxx} 是vscode提供的变量,$ {file} 表示当前文件绝对路径,$ {workspaceFolder} 表示当前工作空间目录绝对路径,$ {fileBasenameNoExtension}表示当前文件不带扩展名的文件名

launch.json

launch.json配置C程序debug

例如:

{
    // 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": [
        {
            "name": "common-debug",
            "type": "lldb",
            "request": "launch",
            "program": "${workspaceFolder}/build/${fileBasenameNoExtension}",
            "args": [],
            "cwd": "${workspaceFolder}",
            "preLaunchTask": "common-build"
        }
    ]
}

name:当前debug任务的代号

type:在成功安装CodeLLDB插件后,才可以使用"lldb"这个type

program:需要debug的程序的绝对路径,必须和task.json中args里的-o参数指定的路径一致

preLaunchTask:必须和task.json的label一致

开始debug

选择要debug的C源文件(必须)

Run -> Start Debugging 或 F5

下面有一个简单的读取标准输入,并将标准输入的字符串反转的程序。我们用它来进行debug演示

创建reverse.c文件,内容是下面的代码,保存。

#include<stdio.h>
#include<stdlib.h>

#define LINE_MAX 100

int getLine(char line[], int maxLine);
void reverse(char line[]);

int main() {
    char line[LINE_MAX];

    while (getLine(line, LINE_MAX) != 0) {
        printf("origin string: %s\n", line);
        reverse(line);
        printf("reverse string: %s\n\n", line);
    }

    return EXIT_SUCCESS;
}

int getLine(char line[], int maxLen) {
    char c;
    int strLen = 0;
    printf("input: ");
    while ((c = getchar()) != EOF) {
        if (c == '\n'){
            line[strLen] = '\0';
            break;
        } else if (strLen >= (maxLen - 1)){
            continue;
        }else {
            line[strLen++] = c;
        }
    }
    return strLen;
}

void reverse(char line[]) {
    int i = 0, j = 0;
    char t;
    while(line[j+1] != '\0') {
        j++;
    }
    while(i < j) {
        t = line[i];
        line[i] = line[j];
        line[j] = t;
        i++;
        j--;
    }
}

调试

### 配置 Mac OS 上的 VSCode 进行 C 语言开发 #### 安装 Xcode 和 Command Line Tools 为了使用 Clang 编译器,在 macOS 中通常已经预安装了该编译器作为 Xcode 命令行工具的一部分。如果尚未安装,则可以通过终端执行 `xcode-select --install` 来获取最新版本的命令行工具[^1]。 #### 安装 Visual Studio Code 及扩展插件 确保已下载并安装好最新的 Visual Studio Code 版本。接着通过 Extensions 视图 (Ctrl+Shift+X 或 Cmd+Shift+X),搜索并安装名为 "C/C++" 的官方 Microsoft 扩展包,这能提供良好的调试体验和支持功能如 IntelliSense[^4]。 #### 设置工作区配置文件 c_cpp_properties.json 创建项目专属的工作空间设置来指定所使用的编译器路径和其他选项。可以在 `.vscode/` 文件夹下的 `c_cpp_properties.json` 文件中定义这些属性: ```json { "configurations": [ { "name": "Mac", "includePath": [ "${workspaceFolder}/**" ], "defines": [], "compilerPath": "/usr/bin/clang", // 指向本地Clang的位置 "intelliSenseMode": "macos-gcc-x64", "cStandard": "gnu17", "cppStandard": "gnu++17" } ], "version": 4 } ``` 上述 JSON 对象中的 `"compilerPath"` 字段指定了要使用的 Clang 编译器位置;对于大多数 macOS 用户而言,默认情况下应位于 `/usr/bin/clang` 处。 #### 创建 tasks.json 构建任务 为了让 VSCode 支持一键构建操作,还需要编辑或新建 `.vscode/tasks.json` 文件如下所示: ```json { "version": "2.0.0", "tasks": [ { "label": "build hello world", "type": "shell", "command": "clang", "args": [ "-g", "-Wall", "-o", "${fileDirname}/${fileBasenameNoExtension}", "${file}" ], "group": { "kind": "build", "isDefault": true }, "problemMatcher": ["$gcc"], "detail": "Generated task to build a single file using the active compiler and output it to the same folder." } ] } ``` 这段代码片段设置了当按下 Ctrl+Shift+B 组合键时触发的任务,它会调用 Clang 将当前打开的源文件编译成同目录下一个具有相同名称但无扩展名的目标文件,并开启警告模式 (`-Wall`) 同时保留调试信息 (`-g`)。 #### 测试配置是否成功 编写简单的 Hello World 程序保存为 .c 文件后尝试运行刚刚建立好的构建任务看能否顺利生成可执行文件。一旦完成以上步骤即代表完成了基于 Clang 的 C 开发环境搭建过程[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值