cmake + vscode + docker 远程调试linux程序

1.安装docker

首先下载docker

https://www.docker.com/get-started

2.编写dockerfile

FROM ubuntu:20.04
ENV DEBIAN_FRONTEND noninteractive

## Update cache and upgrade image
RUN apt-get -y update && apt-get -y upgrade && apt-get -y dist-upgrade

## Build environment packages
RUN apt-get install -qq -y --ignore-missing \
    openssh-server \
    libgtest-dev \
    libbenchmark-dev \
    libprotobuf-c-dev \
    libgrpc++-dev \
    rsync \
    clang  \
    vim \
    lsof  \
    gdb \
    supervisor \
	apt-utils		\
	automake		\
	bc                      \
	build-essential		\
	bzip2			\
	cmake			\
	curl			\
	git			\
	libcurl4-openssl-dev	\
	libssl-dev		\
	libtool-bin		\
	make			\
	pkg-config		\
	protobuf-compiler	\
	libprotobuf-dev         \
	libgmock-dev \
	man-db \
	python			\
	sudo			\
	tar			\
	zip			\
	unzip			\
	wget			\
	zlib1g-dev

## Install cmake since it's an expensive operation and best be done once
RUN mkdir -p /usr/local/bin
RUN mkdir -p /data/test
COPY ./setup-cmake.sh /usr/local/bin/setup-cmake.sh
RUN chmod +x /usr/local/bin/setup-cmake.sh
RUN /usr/local/bin/setup-cmake.sh

RUN mkdir -p /var/log/supervisor

# 修改root密码
RUN echo 'root:root' | chpasswd

#允许rootssh 登录
RUN sed -i "s/#PermitRootLogin no/PermitRootLogin yes/g" /etc/ssh/sshd_config
RUN sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/g" /etc/ssh/sshd_config

RUN mkdir /var/run/sshd

#采用进程管理工具实现镜像run时自动启动ssh服务
COPY supervisord.conf /etc/supervisord.conf
#暴露22端口给宿主机

EXPOSE 22

#启动supervisord服务
CMD ["/usr/bin/supervisord"]

setup-cmake.sh

#!/bin/bash
#
# This script installs latest CMake on Linux machine
#
export PATH=/usr/local/bin:$PATH
# Min required CMake version
export CMAKE_MIN_VERSION=${1:-3.1.0}
# Target version to install if min required is not found
export CMAKE_VERSION=${2:-3.18.4}

UPGRADE_NEEDED=no

function splitVersion {
  pattern='([^0-9]*\([0-9]*\)[.]\([0-9]*\)[.]'
  v1=$(cut -d '.' -f 1 <<< $ver )
  v2=$(cut -d '.' -f 2 <<< $ver )
  v3=$(cut -d '.' -f 3 <<< $ver )
}

function checkVersion {
  # Current CMake version
  currVer=`cmake --version | grep version | cut -d' ' -f 3`
  ver=$currVer splitVersion
  cv1=$v1
  cv2=$v2
  cv3=$v3
  cv=`echo "65536*$v1+256*$v2+$v3" | bc`

  # New CMake version
  ver=$CMAKE_MIN_VERSION splitVersion
  nv=`echo "65536*$v1+256*$v2+$v3" | bc`
  if [ "$cv" -ge "$nv" ]; then
    echo "CMake is already installed: $currVer"
  else
    UPGRADE_NEEDED=yes
  fi
}

checkVersion

if [[ "$UPGRADE_NEEDED" == "no" ]]; then
  echo "Skipping CMake installation"
  exit 0
fi

# Download cmake to /tmp
pushd /tmp
if [[ ! -f "/tmp/cmake.tar.gz" ]]; then
  wget -O /tmp/cmake.tar.gz https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/cmake-${CMAKE_VERSION}.tar.gz
  tar -zxvf /tmp/cmake.tar.gz
fi
# Bootstrap CMake
cd cmake-${CMAKE_VERSION}
./bootstrap --prefix=/usr/local
# Build CMake without CMake and without Ninja (slow)
make
make install
popd

supervisord.conf

[supervisord]

nodaemon=true
[program:sshd]
command=/usr/sbin/sshd -D

3.构建docker 镜像

构建docker

docker build -t opentem-ubuntu:v3 .

运行docker

docker run --security-opt seccomp=unconfined --name test -d -p 10022:22 -v /Users/zhanglei/project/:/data opentem-ubuntu:v3

4.安装要编译的源码

在 /Users/zhanglei/project/ 下载安装库源码

远程登录到docker的服务器

ssh [email protected] -p 10022

安装grpc

git clone [email protected]:grpc/grpc.git

cd grpc

git submodule update --init --recursive

mkdir build

cd build

cmake ..

make -j4

make install

安装opentelemetry

https://github.com/open-telemetry/opentelemetry-cpp.git

cd opentelemetry-cpp

git submodule update --init --recursive

mkdir build

cd build

cmake -DWITH_OTLP=ON  -DWITH_PROMETHEUS=ON ..

make -j4

make install 

5.使用xcode 远程开发

cmakelists.txt配置,主要是帮助gdb找到符号表

cmake_minimum_required(VERSION 3.0.0)
project(demo VERSION 0.1.0)
IF (CMAKE_COMPILER_IS_GNUCC)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
ENDIF (CMAKE_COMPILER_IS_GNUCC)

SET(CMAKE_BUILD_TYPE "Debug")
SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
add_definitions("-Wall -g")
add_executable(demo main.cc)

launch.json

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "demo",
            "type": "cppdbg",
            "request": "launch",
            "program": "${command:cmake.launchTargetPath}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

可以调试了:

你可能感兴趣的:(c,linux,c,c++)