簡體   English   中英

如何在 Windows 上使用 Visual Studio Code 在 Docker 中編譯/調試 C++ 應用程序

[英]How to compile/debug a C++ application in Docker with Visual Studio Code on Windows

我是 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++

這個答案假設您沒有嘗試對多個容器做任何事情......我假設您只想使用一個容器來構建一些 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

這是一個 Dockerfile,它通過添加 GDB 和 gdbserver 擴展了gcc:latest (注意:我不確定是否需要 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 Code: F1Docker: Build Image (pick vsc_docker_cc_gdb:latest)
  • 從 Visual Studio Code: Ctrl + Shift + B構建代碼
  • 從 Visual Studio 代碼: F1任務:運行任務(選擇“刪除容器”)
  • 從 Visual Studio 代碼: F1任務:運行任務(選擇“准備調試”)
  • 從 Visual Studio Code: F5啟動調試器

從那里開始,Visual Studio Code 調試控制台應該可以工作,您應該能夠設置斷點、觀察變量和輸入調試命令。

我在 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 / in /root/.gdbinit以在容器中運行gdb時抑制警告。 安全不應成為當地發展的關注點。

使用docker build -t gdb .構建映像docker build -t gdb . 在工作目錄中,或在 Visual Studio Code 中,從F1Run Task運行預配置的任務build docker gdb

2. 構建應用程序

在項目中,從工作目錄中的 PowerShell 窗口docker run --rm -it -v ${pwd}:/work --workdir /work gcc make debug 使用 Visual Studio Code,這可以通過F1Run Task 中的預配置任務make debug來完成。

3. 調試應用程序

您希望將 Visual Studio Code 配置為從容器內運行/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
                }
            ]
        }
    ]
}

使用此設置,您需要做的就是在調試工作區中按下播放鍵。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM