DEV Community

Cover image for Dia 23 - Configurando o NixOS para utilizar o debbuger de C no VSCode
Matheus Gomes
Matheus Gomes

Posted on

Dia 23 - Configurando o NixOS para utilizar o debbuger de C no VSCode

Eu instalo meus pacotes todos em environment.systemPackages no /etc/nixos/configuration.nix.

Então, além do gcc, para utilizar o debbuger é necessário:

environment.systemPackages = with pkgs; [
...
vscode-fhs
gdb
]
Enter fullscreen mode Exit fullscreen mode

Utilizar o vscode-fhs facilita muito o trabalho, instalar o pacote vscode (sem o fhs) gera alguns transtornos. Mais informações aqui.

No vscode, instale essa extensão:

extensão vscode c/c++

Para maior confiabilidade, "forcei" a criação de paths com:

environment.variables.PATH = "${lib.makeBinPath [
pkgs.gdb
pkgs.gcc_multi // esse é o pacote que utilizo pro gcc
]}:$PATH";
Enter fullscreen mode Exit fullscreen mode

Não esqueça de reiniciar o computador após o sudo nixos-rebuild switch

Dentro do projeto, crie uma pasta .vscode e adicione um arquivo launch.json e tasks.json

tasks.json:

{
  "tasks": [
    {
      "type": "cppbuild",
      "label": "C/C++: gcc arquivo de build ativo",
      "command": "/nix/store/2ap4rlj55kw8q5ndycl0r8w312ggpf1c-gcc-wrapper-13.2.0/bin/gcc",
      "args": [
        "-fdiagnostics-color=always",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}"
      ],
      "options": {
        "cwd": "${fileDirname}"
      },
      "problemMatcher": [
        "$gcc"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "detail": "Tarefa gerada pelo Depurador."
    }
  ],
  "version": "2.0.0"
}
Enter fullscreen mode Exit fullscreen mode

launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug arquivo ativo",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}",
      "args": [],
      "cwd": "${fileDirname}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
      "miDebuggerPath": "/nix/store/2ap4rlj55kw8q5nd2460r8w312ggpf1c-gdb-14.2/bin/gdb",  
      "preLaunchTask": "C/C++: gcc arquivo de build ativo",
      "internalConsoleOptions": "openOnSessionStart"
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

Para o campo command do tasks.json utilize o comando which gcc para pegar o caminho.
Faça o mesmo para miDebuggerPath do launch.json com o comando which gdb

Entre no arquivo que deseja depurar e aperte F5:
imagem do depurador funcionando

Depure e seja feliz.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

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

Okay