Skip to content

Instantly share code, notes, and snippets.

@danielTobon43
Last active June 4, 2022 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielTobon43/9dc4f0433a2cbade8c345fa8975e25ee to your computer and use it in GitHub Desktop.
Save danielTobon43/9dc4f0433a2cbade8c345fa8975e25ee to your computer and use it in GitHub Desktop.
Debug C++ application in Docker container with vscode

Debugging c++ project in docker container with vscode

You can attached a vscode session in a running docker container or vscode can create a docker container to attach.

image

For this configuration we are going to run a docker container in detached mode and then, connect to it using vscode in remote mode.

  • Running container must have installed gdb
  • Install Remote-containers extensions in your local vscode application
docker run --rm -d -p 12345:2225 -it \
           --name="docker-container" \
           --volume=/home/user/Downloads/project:/tmp/project \
           YOUR-DOCKER-IMAGE

vscode

  1. Click on Remote explorer (left bar)
  2. You will see the running docker container. For this example we will be using a docker image danieltobon43/pcl-docker:1.12.1-alpine3.15-dev-v4

Screenshot from 2022-06-03 17-27-12

  1. Press on Attach or Open folder in container. See Documentation for more information.

vscode extensions

The following vscode extensions must be install in the vscode-remote version of the running container

  • C/C++
  • C/C++ Extension Pack
  • CMake
  • CMake Tools

Note

Remove any build folder you might have and then press the Debug button. This is because vscode CMake-Tools extension will create a build folder with debug symbols and if there is already a build/ folder you might find a permission error such as:

Error: EACCES: permission denied

Debugging

You can debug your application with a custom launch.json file or using CMake-Tools.

Debugging: launch.json

  1. Create a launch.json file in the .vscode folder at the root of your project. example-json
  2. Run and Debug (On the left panel) and choose CMakeDebugLaunch, play button

Debugging: CMake-tools

  1. Click on the CMake-tools (On the left panel) and then, click right to show a new menu. Select debug

Enjoy

Now you can set a breakpoint in your remote-vscode session and start debugging your cpp application

Docker documentation

{
"version": "0.2.0",
"configurations": [
{
"name": "CMakeDebugLaunch",
"type": "cppdbg",
"request": "launch",
//"coreDumpPath": "GDB",
// Resolved by CMake Tools:
"program": "${command:cmake.launchTargetPath}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/build",
"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}"
},
{
"name": "OTHER_VALUE",
"value": "Something something"
}
],
"externalConsole": false,
"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