如何在Windows上使用Visual Studio Code在Docker中编译/调试C ++应用程序

如何在Windows上使用Visual Studio Code在Docker中编译/调试C ++应用程序

问题描述:

我是Visual Studio Code和Docker的新手。现在,我想使用Visual Studio Code编辑我的C ++代码,并使用Docker进行编译/调试。

I'm new on Visual Studio Code and Docker. Now I want to use Visual Studio Code to edit my C++ code and Docker to compile/debug.

我不知道如何编写launch.json和任务。 json文件正确,以便我可以在Visual Studio Code开发环境下使用Docker编译/调试我的C ++应用程序。

I don't know how to write the launch.json and task.json correctly files, so that I can use Docker to compile/debug my C++ application just under Visual Studio Code development environment. Is there a solution for this problem?

这是我的平台信息:

操作系统:Windows 10

Visual Studio代码:v1.25.1
Docker中的
操作系统:Ubuntu  16.04(Xenial Xerus)
Docker中的
编译器:g ++

OS: Windows 10
Visual Studio Code: v1.25.1
OS in Docker: Ubuntu 16.04 (Xenial Xerus)
Compiler in Docker: g++

此答案假设您不打算对多个容器进行任何操作...我假设您只想使用一个容器来构建一些C ++代码,并且所有代码都位于名为 C:\vsc_docker_cc_gdb 的文件夹中。我还假定您在Visual Studio Code中安装了Microsoft的C ++和Docker扩展。

This answer assumes that you are not trying to do anything with multiple containers... I'm assuming that you just want to use a single container to build some C++ code, and that all of your code is in a folder called C:\vsc_docker_cc_gdb. I also assume you have the C++ and Docker extensions from Microsoft installed in Visual Studio Code.

让我们从一个简单的C ++文件开始,该文件名为hello.cc:

Let's start with a simple C++ file, called hello.cc:

#include <iostream>
int main(int argc, char **argv) {
  std::cout << "Hello from Docker" << std::endl;
}

我们还要添加一个Makefile:

Let's also add a Makefile:

CXXFLAGS = -O3 -ggdb -m64
LDFLAGS  = -m64

all: hello.exe
.PRECIOUS: hello.exe hello.o
.PHONY: all clean

%.o: %.cc
    $(CXX) -c $< -o $@ $(CXXFLAGS)

%.exe: %.o
    $(CXX) $^ -o $@ $(LDFLAGS)

clean:
    rm -f hello.o hello.exe

这是一个扩展了 gcc:latest 通过添加GDB和gdbserver(注意:我不确定是否需要gdbserver):

Here's a Dockerfile that extends gcc:latest by adding GDB and gdbserver (note: I'm not sure gdbserver is needed):

FROM gcc:latest
LABEL Name=vsc_docker_cc_gdb Version=0.0.2
RUN apt-get -y update
RUN apt-get -y install gdb gdbserver
WORKDIR /root

这里是.vscode / tasks.json:

Here's .vscode/tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build (in container)",
            "type": "shell",
            "command": "docker run --privileged -v c:/vsc_docker_cc_gdb/:/root vsc_docker_cc_gdb make",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": [
                    "relative",
                    "${workspaceFolder}"
                ],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        },
        {
            "label": "clean (in container)",
            "type": "shell",
            "command": "docker run --privileged -v c:/vsc_docker_cc_gdb/:/root vsc_docker_cc_gdb make clean",
            "group": "build",
            "problemMatcher": []
        },
        {
            "label": "remove containers",
            "type": "shell",
            "command": "docker ps -a -q | % { docker rm $_ }",
            "problemMatcher": []
        },
        {
            "label": "run the code",
            "type": "shell",
            "command": "docker run --privileged -v c:/vsc_docker_cc_gdb/:/root vsc_docker_cc_gdb ./hello.exe",
            "group": "build",
            "problemMatcher": []
        },
        {
            "label": "prepare to debug",
            "type": "shell",
            "command": "docker run --privileged -v c:/vsc_docker_cc_gdb/:/root --name debug_vsc -it vsc_docker_cc_gdb ",
            "group": "build",
            "problemMatcher": []
        }
    ]
}

最后,.vscode / launch.json:

And finally, .vscode/launch.json:

{
    "version": "0.2.0",
    "configurations": [{
        "name": "(gdb) Pipe Launch",
        "type": "cppdbg",
        "request": "launch",
        "program": "/root/hello.exe",
        "cwd": "/root",
        "args": [],
        "stopAtEntry": true,
        "environment": [],
        "externalConsole": true,
        "pipeTransport": {
            "debuggerPath": "/usr/bin/gdb",
            "pipeProgram": "docker.exe",
            "pipeArgs": ["exec", "-i", "debug_vsc", "sh", "-c"],
            "pipeCwd": "${workspaceRoot}"
        },
        "MIMode": "gdb",
        "setupCommands": [{
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
        }]
    }, ]
}

这里有两件重要的事情。首先,您会注意到launch.json的一部分引用了容器(/ root /)中的路径,而其他部分则引用了Windows主机(workspaceRoot)上的路径。这很重要。

There are two important things here. The first is that you'll notice that parts of launch.json are referring to paths in the container (/root/) and others are referring to paths on the Windows host (workspaceRoot). That is important.

第二个是您需要运行一个容器,然后可以在其中启动调试过程 。这是从零开始到启动该特殊容器并在其中启动调试器的方法。

The second is that you'll need to have a container running, and then you can launch a debug process into it. Here's a recipe to go from zero to starting that special container and launching a debugger in it.


  • 在PowerShell中: docker pull gcc

  • 从Visual Studio代码中: F1 Docker:构建映像(选择vsc_docker_cc_gdb:最新)

  • 从Visual Studio代码中: Ctrl + Shift + B 构建代码

  • 从Visual Studio代码: F1 任务:运行任务(选择删除容器)

  • 从Visual Studio代码中: F1 任务:运行任务(选择准备调试)

  • 从Visual Studio代码中: F5 启动调试器

  • From PowerShell: docker pull gcc
  • From Visual Studio Code: F1, Docker: Build Image (pick vsc_docker_cc_gdb:latest)
  • From Visual Studio Code: Ctrl + Shift + B to build the code
  • From Visual Studio Code: F1, Tasks: Run Task (pick "remove containers")
  • From Visual Studio Code: F1, Tasks: Run Task (pick "prepare to debug")
  • From Visual Studio Code: F5 to start the debugger

从此处开始,Visual Studio Code Debug Console应该可以工作了,您应该能够设置断点,监视变量并输入调试命令。

From there, the Visual Studio Code Debug Console should work, and you should be able to set breakpoints, watch variables, and enter debug commands.