DEV Community

Cover image for Debugging code in C language with a Makefile on MAC
Traky Richard BLM
Traky Richard BLM

Posted on

5

Debugging code in C language with a Makefile on MAC

Debugging its code remains a fundamental step in solving bugs. Some programs seem less bulky in terms of a line of code and don't really require the need for debugging. On the other hand, others contain several hundred lines of code, and it becomes almost impossible to fix bugs without debugging.

In this tutorial I will explain how to debug in C with a configured makefile.

The debugging option is by default integrated in visual studio,
In C to debug we need to install an extension that you will find on this link.

Once the extension is installed it is now time to start the configuration.

Create a .vscode folder
In this folder create a launch.json file in which you will put the code below

{
    “version”: “0.2.0”,
    “configurations”: [
        {
            “name”: “C/C++: clang++ build and debug active file”,
            “type”: “cppdbg”,
            “request”: “launch”,
            “program”: “${workspaceFolder}/[execName]”,
            “args”: [arg1, arg2],
            “stopAtEntry”: true,
            “cwd”: “${workspaceFolder}“,
            “environment”: [],
            “externalConsole”: false,
            “MIMode”: “lldb”,
            “preLaunchTask”: “make re”
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

In summary, this config allows us to set up our debugger by giving it a name, the program to run, the arguments to pass etc...
The most important things to notice and modify are the name of your program and the various arguments you pass when running your program.

NB: The list of arguments must be in comma separated strings.

Only one last step
Have you noticed that the last line of our launch.json file contains an attribute “preLaunchTask”: “make re”, this line will be useful in the step below.

Let's create another tasks.json file in the same .vscode folder located at the root of your project, in this folder we will define some tasks to execute before launching our debugger.
A simplified version of this file should look like this.

{
    “version”: “2.0.0”,
    task:
    [
        {
            “label”: “make re”,
            “type”: “shell”,
            “command”: “make”,
            “args”: [“re”]
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

NB: The label and the preLaunchTask must be identical to create the link.

To summarize this config allows us to launch our command make followed by an argument re which means "rebuild" so that the new instance of debugger contains the latest changes.

NB: For breakpoints to work you must include the #-g# attribute among the compilation flags of your project, for example: >gcc -g -wall -wextra etc...
otherwise all the work done so far will be in vain.

We're all good, all you have to do is click on the third tab on the left of your editor to launch your debugger.

debugger image

Happy coding to you, and see you next week for a new article.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay