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

时间:2018-07-20 01:43:51

标签: c++ docker visual-studio-code

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

我不知道如何正确编写launch.json和task.json文件,因此我只能在Visual Studio Code开发环境下使用Docker来编译/调试C ++应用程序。有解决这个问题的方法吗?

这是我的平台信息:

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

2 个答案:

答案 0 :(得分:2)

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

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

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

我们还要添加一个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

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

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:

{
    "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:

{
    "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)上的路径。那很重要。

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

  • 从PowerShell:docker pull gcc
  • 从Visual Studio代码中: F1 Docker:构建映像(选择vsc_docker_cc_gdb:latest)
  • 从Visual Studio代码中: Ctrl + Shift + B 构建代码
  • 从Visual Studio代码: F1 任务:运行任务(选择“删除容器”)
  • 从Visual Studio代码: F1 任务:运行任务(选择“准备调试”)
  • 从Visual Studio代码: F5 启动调试器

从那里开始,Visual Studio代码调试控制台应该可以工作,并且您应该能够设置断点,监视变量并输入调试命令。

答案 1 :(得分:1)

我在GitHub上建立了一个最小的工作示例:https://github.com/fschwaiger/docker-cpp-vscode

假设您使用ms-vscode.cpptools扩展名,想法如下:

  1. 您需要安装了gccgdb的容器(可以相同)
  2. 您在容器中构建应用程序
  3. 您在容器内运行gdb

1。获取gccgdb

的图像

gcc可直接从Docker Hub:docker pull gcc获得。我在那里没有找到gdb,因此有一个用于构建它的Dockerfile:

FROM gcc:latest
RUN apt-get update && apt-get install -y gdb
RUN echo "set auto-load safe-path /" >> /root/.gdbinit

它基于gcc:latest构建并安装gdb,因此您可以使用同一映像进行编译和调试。它还在set auto-load safe-path /中设置选项/root/.gdbinit,以在容器中运行gdb时禁止显示警告。安全不应该是当地发展的问题。

在工作目录中使用docker build -t gdb .构建映像,或者在Visual Studio Code中,从 F1 运行任务运行预配置的任务build docker gdb

2。构建应用程序

在项目中,从工作目录中的PowerShell窗口运行docker run --rm -it -v ${pwd}:/work --workdir /work gcc make debug。使用Visual Studio Code,可以通过 F1 运行任务中的预配置任务make debug完成。

3。调试应用程序

您要配置Visual Studio代码以从容器中运行/usr/bin/gdb。您可以为此使用pipeTransport中的launch.json选项并使其运行:

docker run --rm --interactive --volume ${workspaceFolder}:/work --workdir /work --privileged gdb sh -c /usr/bin/gdb

说明:

  • --privileged:允许二进制调试
  • --volume ${workspaceFolder}:/work --workdir /work:将项目文件夹安装到容器中
  • --rm:退出后删除容器
  • --interactive:VSCode将向gdb shell发出交互式命令
  • sh -c:定义GDB中运行的shell入口点

总体launch.json如下所示。请注意,programcwd是容器内 的路径。 sourceFileMap允许调试器将断点与源文件进行匹配。其余的是C ++扩展中的默认模板。

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Docker",
            "type": "cppdbg",
            "request": "launch",
            "program": "build/apps/program",
            "args": [],
            "stopAtEntry": true,
            "cwd": "/work",
            "environment": [],
            "externalConsole": true,
            "preLaunchTask": "make debug",
            "targetArchitecture": "x64",
            "sourceFileMap": { "/work": "${workspaceFolder}" },
            "pipeTransport": {
                "debuggerPath": "/usr/bin/gdb",
                "pipeProgram": "docker.exe",
                "pipeArgs": ["run","--rm","--interactive","--volume","${workspaceFolder}:/work","--workdir","/work","--privileged","gdb","sh","-c"],
                "pipeCwd": ""
            },
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

使用此设置,您只需在调试工作区中按一下播放即可。