【问题标题】:How do I build and run C files that use math.h functions in VSCode?如何在 VSCode 中构建和运行使用 math.h 函数的 C 文件?
【发布时间】:2020-03-07 15:54:22
【问题描述】:

正如这里提到的:Undefined reference to pow( ) in C, despite including math.h,我可以通过将-lm 放在gcc -o namefile namefile.c 的末尾来构建仅在终端中在 Linux Ubuntu 中使用 math.h 函数的 C 文件。但我想构建和运行一个专门在 VSCode 中使用 math.h 的 C 代码。我该怎么做?

【问题讨论】:

  • 将其添加到您的库中。
  • 您究竟是如何“在 VSCode 中构建和运行 C 代码”的?你是否使用一些扩展来为你运行编译器?
  • 我通过按 F6 构建和运行它们,我使用 C 的扩展名“Code Runner”、“C/C++ Compile Run”和“C/C++”。

标签: c visual-studio-code


【解决方案1】:

您可以在 VS Code 中使用 custom task configuration 执行相同的操作来编译您的 .c 文件。

假设我们有这个带有 math.h 的 test.c 文件。

#include <math.h>
#include <stdio.h>

#define PI 3.14159265 //defines the value of PI

/* Calculate the volume of a sphere from a given radius */
double volumeFromRadius(double radius) {
    return (4.0/3.0) * PI * pow(radius,3.0f);
}

int main(void) {
    double radius = 5.1;
    printf("The volume for radius=%.2f is %.2f", radius, volumeFromRadius(radius));
}

第 1 步:创建 tasks.json 文件

这是用于编译/构建您的代码。
要自动创建:

  1. 打开 .c 文件
  2. 打开命令面板
  3. 选择 C/C++: Build and Debug Active File(由 C/C++ 扩展添加)
  4. 选择你的编译器(例如我的是gcc-7

这将自动创建一个 tasks.json 文件并尝试编译您的 .c 文件,我们预计该文件会失败,因为它缺少 -lm 标志。因此,编辑 tasks.json 文件的内容:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Compile test.c",
            "type": "shell",
            "command": "/usr/bin/gcc-7",
            "args": [
                "-g",
                "-o",
                "${workspaceFolder}/Q/test.out",
                "${workspaceFolder}/Q/test.c",
                "-lm"
            ]
        }
    ]
}

在这里,我将-lm 标志添加到gcc 参数并将label 编辑为“编译test.c”。适当修改 .c 和 .out 文件的路径以匹配您的环境。

有关架构的更多信息:https://code.visualstudio.com/docs/editor/tasks#_custom-tasks

第 2 步:创建一个 launch.json 文件

这是用于运行您的代码。
要自动创建:

  1. 打开命令面板
  2. 选择调试:打开launch.json
  3. 选择C++ (GDB/LLDB)

然后对其进行编辑以运行预期的 .out 文件。

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Run test.c",
            "preLaunchTask": "Compile test.c",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/Q/test.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}/Q",
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        },
    ]
}

注意preLaunchTask 应该在tasks.json 中指定相同的任务标签。同样,适当地修改路径以匹配您的环境,尤其是 .out 文件的路径和文件名。

第 3 步:编译并运行它

现在,我不使用(或不喜欢)Code Runner。
我使用 VS Code 的内置调试器配置。

单击左侧的调试器,然后从下拉列表中选择“Run test.c”。

这应该编译您的 .c 文件,运行它,并将任何输出打印到终端面板。

默认情况下,焦点转到运行输出。但如果您还想查看编译/构建日志,您可以从下拉列表中选择任务。

【讨论】:

    猜你喜欢
    • 2020-12-19
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-06
    • 2018-09-22
    • 1970-01-01
    相关资源
    最近更新 更多