首页 文章

无法在vscode中调试单个.c文件

提问于
浏览
0

我认为应该是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.

非常感谢你的帮助

1 回答

  • -1

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

相关问题