Home > Backend Development > C++ > body text

Day - Configuring NixOS to use the C debbuger in VSCode

Patricia Arquette
Release: 2024-10-22 13:34:02
Original
630 people have browsed it

I install all my packages in environment.systemPackages in /etc/nixos/configuration.nix.

So, in addition to gcc, to use debbuger you need:

environment.systemPackages = with pkgs; [
...
vscode-fhs
gdb
]
Copy after login

Using vscode-fhs makes the work much easier, installing the vscode package (without fhs) causes some inconvenience. More information here.

In vscode, install this extension:

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

For greater reliability, I "forced" the creation of paths with:

environment.variables.PATH = "${lib.makeBinPath [
pkgs.gdb
pkgs.gcc_multi // esse é o pacote que utilizo pro gcc
]}:$PATH";
Copy after login

Don't forget to restart your computer after sudo nixos-rebuild switch

Within the project, create a .vscode folder and add a launch.json and tasks.json file

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"
}
Copy after login

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"
    }
  ]
}
Copy after login

For the command field in tasks.json, use the which gcc command to get the path.
Do the same for launch.json's miDebuggerPath with the command which gdb

Enter the file you want to debug and press F5:
Dia - Configurando o NixOS para utilizar o debbuger de C no VSCode

Debug and be happy.

The above is the detailed content of Day - Configuring NixOS to use the C debbuger in VSCode. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!