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

时间:2018-08-16 17:26:18

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

我认为应该花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和task.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"
        }
    ]
}

我的task.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 个答案:

答案 0 :(得分:-1)

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