【问题标题】:Unable to debug a single .c file in vscode无法在 vscode 中调试单个 .c 文件
【发布时间】:2019-01-23 17:33:21
【问题描述】:

我想我处理这个问题应该是 6 个小时!无论如何,我想在 linux ubuntu 64bit 的 vscode 中构建一个 .c 文件。这是我的代码(quadratic-equation.c):

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

double quadratic_equation(float a, float b, float c);

int main()
{
    float x, a, b, c;

    printf("Enter a: ");
    scanf("%f", &a);

    printf("Enter b: ");
    scanf("%f", &b);

    printf("Enter c: ");
    scanf("%f", &c);

    x = quadratic_equation(a, b, c);
    printf("form: ax%c + bx + c = 0:\n", 253);
    printf("x = %f", x);

    return 0;
}

double quadratic_equation(float a, float b, float c)
{
    float x;

    if(a == 0)
    {
        if(b == 0)
        {
            printf("a & b can not be both zero");
        }
        else
        {
            x = -c / b;
            return x;
        }
    }
    if((b * b - 4 * a * c ) == 0)
    {
        x = -b / (2 * a);
        return x;
    }

    x = (-b + sqrt(b * b - 4 * a * c)) / 2 * a;
    return x;
}

我已经解决了这段代码中的所有问题。 在构建的路上,VSCode强迫我创建了3个文件,c_cpp_properties.json、launch.json和tasks.json。 所以,我在这里复制粘贴它们: 我的 c_cpp_properties.json 文件:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "/usr/include",
                "/usr/local/include",
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE"
            ],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64",
            "browse": {
                "path": [
                    "${workspaceFolder}"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        }
    ],
    "version": 4
}

我的 launch.json 文件:

{
    // 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": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/quadratic-equation.c",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build quadratic equation"
        }
    ]
}

我的 tasks.json 文件:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build quadratic equation",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g", "quadratic-equation.c"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

这是 gdb --version 的输出:

GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".

我也尝试添加没有帮助的makefile文件。(我实际上不知道如何配置它,但我搜索了一下并尝试了一些,但没有帮助)。 最后,这是我按 f5(开始调试)时遇到的错误:

Unable to start debugging. Program path '/home/amirali/XPSC/test/c-cpp/quadratic-equation.c' is missing or invalid.

GDB failed with message: "/home/amirali/XPSC/test/c-cpp/quadratic-equation.c": not in executable format: File format not recognized

This may occur if the process's executable was changed after the process was started, such as when installing an update. Try re-launching the application or restarting the machine.

非常感谢您的帮助

【问题讨论】:

  • 我猜这行:"program": "${workspaceFolder}/quadratic-equation.c", 应该指向编译后的可执行文件而不是源文件。
  • 您必须使用可执行文件而不是源文件调用 gdb。
  • @EugeneSh。如何编译我的代码?这其实是我的问题!
  • 你提出的问题不是编译问题,而是调试问题。
  • @EugeneSh。那么如何在 vscode 中编译我的代码呢?

标签: c ubuntu debugging gcc visual-studio-code


【解决方案1】:

终于找到答案了。 在工作文件夹中运行gcc -g -o {executableDesiredFileName} {yourFile}.c -lm 并将launch.json 中的路径“program”运行到可执行文件(-lm 将尝试包含额外的头文件,-o 将重命名已编译的程序,默认为 a.out )。

【讨论】:

    猜你喜欢
    • 2021-12-22
    • 2021-02-15
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    相关资源
    最近更新 更多