Jenniffer Lawrance
Jenniffer Lawrance

Reputation: 107

How to setup Visual Studio Code to debug C programs on Linux?

I am trying to debug a C program in Visual Studio Code.
In my directory I have 2 files test.c and Makefile along with .vscode folder which contains launch and tasks json files.
I tried configuring these files for the last three hours searching various forums and blogs but nothing seems to work.

I am able to compile and run using these two json files.
Program runs and displays output correctly but doesn't stop at breakpoints, during execution of the program I can't add breakpoints and already added breakpoints are disabled with following message.

Module containing this breakpoint has not yet loaded or the breakpoint address could not be obtained.

It seems that VSCode is not able to locate my test.c file during debugging phase even though it is in the same directory. It will be great if someone can show me the right way to do it.
Here, I am attaching contents of files in my folder.
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": "gcc build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/test",
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "tasks",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

tasks.json

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

Makefile

all:
    gcc test.c -o ./test

test.c

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

int main(){
    printf("Mandar\n");
    printf("Sadye\n");
    return 0;
}

Thank you.

Upvotes: 5

Views: 13086

Answers (1)

Scott McPeak
Scott McPeak

Reputation: 12769

Your configuration is correct except for one tiny thing: you forgot to pass the -g flag to gcc. Consequently, the test program does not have debugging information in it, and therefore gdb does not know the relationship between the source code and the compiled program.

Also, targets in a Makefile should specify what files they depend on. Your all target does not have a dependency on test.c, so changing the source code will not cause recompilation.

Here is a fixed Makefile:

all: test

test: test.c
        gcc -g test.c -o ./test

With that fix, I am able to compile and debug this program on Linux using VSCode 1.36.1.

Upvotes: 9

Related Questions