注:本文想实现vscode在docker中debug c++,找了一些教程,但都没有完整的解决方案,在github上找到了一个解决方法,建议直接看这篇文章gjthub:Building and debugging C++ applications in a Docker container using Visual Studio Code.,我也将自己的实践过程写了过来,我的能够编译运行,但是到编译debug不能进入断点,如果您阅读之后有解决方案,请评论或私信。
更改一下主题
linux配置
查看一下系统版本
/tmp # cat /etc/issue
Welcome to Alpine Linux 3.10
因为之前的系统为Alpine ,但是有以下提示,所以从新做一个linux系统
- docker search ubuntu:20.04
PS C:\Users\Administrator\Desktop\test> docker search ubuntu:20.04
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
manishfoodtechs/xfcefulldesktop_ubuntu20.4 Full Desktop In Docker : Ubuntu:20.04 5
delta42/ubuntu-with-openjdk-13 Ubuntu:20.04 with OpenJDK13 JRE installed 1
hexiaoyuan/pete-dev-base ubuntu:20.04: include sshd, vim, gcc, python… 0
mikxox/maven-test-action Image based upon ubuntu:20.04, includes java… 0
julianqz/ubuntu Minimal Ubuntu:20.04 0
bubuntux/s6-ubuntu Multi-platform Ubuntu:20.04 image with s6-ov… 0
mooping/apache2-php7 Base on ubuntu:20.04 0
artemkrim/universal-base-image ubuntu:20.04 with cli-tools 0
best7777/ubus ubuntu:20.04 ssh and rdp and firefox instal… 0
sylketri/ubuntu20.04-latex LaTex Docker image based on ubuntu:20.04 0
subha987/sshcontainer this uses ubuntu:20.04 as base image. this c… 0
taiansu/ubuntu-phoenix-builder An Ubuntu:20.04 with necessary packages for … 0
wagner85/jks-slave ubuntu:20.04 - Jenkins slave 0
julianqz/cellranger-6.0.1 Cell Ranger v6.0.1 from 10x Genomics. Built … 0
n0bility6/nginx-php ubuntu:20.04 with nginx, php and composer fo… 0
eiorobot/openjdk ubuntu:20.04&openjdk11 0
datica/dcc-bastion2 Basic Ubuntu:20.04 image for utility use in … 0
ixybus/ubvimgit Ubuntu:20.04 with Vim and Git installed 0
hanlinyang/openjdk AdoptOpenJDK based on hanlinyang/ubuntu:20.0… 0
alikazemkhanloo/miktex miktex on ubuntu:20.04, focal 0
eirture/mongosh the mongosh image base on ubuntu:20.04 0
mastack/tbuilder this repository is made as a ready-to-use ub… 0
alby117/ubuntu_full Ubuntu:20.04 with software-properties-common… 0
aghaahmadi/mypython ubuntu:20.04 Python 3.8.10 wkhtmltopdf panda… 0
ariqbasyar/opencv OpenCV & NumPy python with ubuntu:20.04 base… 0
- docker pull manishfoodtechs/xfcefulldesktop_ubuntu20.4
- docker images
PS C:\Users\Administrator\Desktop\test> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
manishfoodtechs/xfcefulldesktop_ubuntu20.4 latest 5fc36d410b62 23 months ago 4.44GB
- docker run -it --name test -v C:/Users/Administrator/Desktop/test:/tmp -p 50051:50051 5fc36d410b62
- 在容器中:
- apt install build-essential 关联软件
- gcc -v
- gdb -v # 如果没有 sudo apt-get install gdb
- apt -install cmake
- cmake -version
安装插件
安装插件 Remote-containers
- 或者安装这个(新版本)
点击左下角的对向箭头
- 在打开的命令窗口中选择“attach to running container”(开启容器命令docker container start container_id)
- 在弹出的选项中选择将要使用的容器即可
-
然后点击会进入一个新的界面
-
ctrl+~
调用镜像的terminal
测试环境
- 打开远程文件夹
CMakeLists.txt 文件,cmake 的所有配置都在这个文件中完成,CMakeLists.txt 中的内容大致如下:(指令是不区分大小写的)
cmake_minimum_required(VERSION 3.10)
project(SafeIntAdd) // 工程ming+支持的语言(默认所有语言) project(SafeIntAdd C CXX)
//为下边的语句设定变量
set(CMAKE_CXX_COMPILER "c++")
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_FLAGS -g -Wall)
message(STATUS "CMAKE_CXX_FLAGS: " "${CMAKE_CXX_FLAGS}")
message(STATUS "CMAKE_CXX_FLAGS: " "${CMAKE_CXX_FLAGS}")
string(REPLACE ";" " " CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
add_executable(exename main.cpp) //
编辑好 CMakeLists.txt 之后,我们可以新建一个 build 目录,并在 build 目录下使用 cmake 来进行构建,构建成功的话再使用 make 来进行编译和链接,最终得到 SafeAdd 这个可执行文件:
$ mkdir build/
$ cd build/
$ cmake ..
$ make // 编译 executable
$ ./exename
点击后得到:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
]
}
]
}
编译成debug版本
- g++ -g main.cpp
- 后生成a.out
- 正常的话点击RUN旁边的三角形就可进入dubug了