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

问题描述 投票:3回答:2

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

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

这是我的平台信息:

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

c++ docker visual-studio-code
2个回答
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:最新)
  • 从Visual Studio代码:Ctrl + Shift + B来构建代码
  • 从Visual Studio代码:F1,任务:运行任务(选择“删除容器”)
  • 从Visual Studio代码:F1,任务:运行任务(选择“准备调试”)
  • 从Visual Studio代码:F5启动调试器

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


1
投票

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

假设你有ms-vscode.cpptools扩展,这个想法如下:

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

1. Get the images for gcc and gdb

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→Run Task运行预配置的任务build docker gdb

2. Building the application

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

3. Debug the application

您希望配置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
                }
            ]
        }
    ]
}

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

© www.soinside.com 2019 - 2024. All rights reserved.