Skip to content

Instantly share code, notes, and snippets.

@sergio-gimenez
Last active April 26, 2023 14:07
Show Gist options
  • Select an option

  • Save sergio-gimenez/79e0afec464eb70fc70dbd0d42e3f233 to your computer and use it in GitHub Desktop.

Select an option

Save sergio-gimenez/79e0afec464eb70fc70dbd0d42e3f233 to your computer and use it in GitHub Desktop.
CMake C Debugging: This allows to debug C programs using cmake as build system in VScode. CMake Tools extension must be installed in VScode.
# Solution and discussion in https://stackoverflow.com/questions/40033311/how-to-debug-programs-with-sudo-in-vscode
# Make sure gdb is installed in the system
# Place this script in your home directory (or wherever, but the launch.json will look to the home dir) to debug programs that require root privileges.
# Make sure the script is made executable. (chmod +x gdb)
# For local development (will open a dialog for entering password)
pkexec /usr/bin/gdb "$@"
# For remote development (add gdb to /etc/sudoers, hence it can be run without password)
# How to do it: https://linuxhandbook.com/sudo-without-password/
# sudo /usr/bin/gdb "$@"
# Install C and Cmake extensions in vscode and make sure the cmake project is executed by vscode (clean nad reconfigure project) and build by vscode
// Add this launch.son file in the .vscode folder of your project.
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
// Resolved by CMake Tools:
"program": "${command:cmake.launchTargetPath}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [
{
// add the directory where our target was built to the PATHs
// it gets resolved by CMake Tools:
"name": "PATH",
"value": "$PATH:${command:cmake.launchTargetDirectory}"
},
],
"externalConsole": false,
// Path to the "gdb" script
"miDebuggerPath": "/home/<username>/gdb",
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment