0%

(Linux)(VSCode)(Docker) Visual Studio Code use remote conainter to develop C and CPP program.

安裝操作

使用docker buildfile建立VSCode C/CPP docker Image

步驟

  1. Microsoft Dockerhub取得VSCode C/CPP docker

    1
    2
    # Docker buildfile
    FROM mcr.microsoft.com/vscode/devcontainers/cpp:ubuntu-20.04
  2. 安裝GCC/GDB toolchain, CMake

    1
    2
    3
    4
    # Docker buildfile
    RUN apt-get --yes install build-essential
    RUN apt-get --yes install gdb
    RUN apt-get --yes install cmake
  3. 使用範例build.sh安裝Image,安裝完成後,使用範例run.sh執行container

  4. 打開Vscode,選取Remote Explorer->Containers->Dev Containers->C(範例Container name),滑鼠右鍵點選Attach to Container

  5. VsCode打開工作區File->Open Folder

  6. 編寫程式File->New File->main.c

1
2
3
4
5
6
7
#include <stdio.h>
#include <stdlib.h>

void main(void)
{
printf("hello world\n");
}
  1. 執行程式Run->Run Without Debug

    VSCode CMake

  2. 完成上面的Vscode C docker container
  3. 安裝VSCode CMake Extension,Extensions->Search->CMake, CMake Tools
  4. 設定CMake toolchain,View->Command Palette->CMake:Select a Kit->GCC
    或者點選VSCode視窗最下方狀態列工具圖示
  5. 設定CMake Build Target,View->Command Palette->CMake:Set Build Target->Folder name[Executable]
    或者點選VSCode視窗最下方狀態列三角形圖示
  6. 設定CMake Debug,View->Command Palette->CMake:Set Debug Target
  7. 執行程式Run->Run Without Debug
  8. 除錯程式Run->Debug,可在Debug console使用-exec <gdb command>進行除錯,也可透過GUI工具

    範例

Reference