星期四, 10月 10, 2024

GCP Cloud Shell Editor 與 C 編譯小記

GCP Cloud Shell Editor 與 C 編譯小記



今天來寫使用 GCP 的 Cloud Shell Editor 小記


先來寫一下為何會寫這篇的情境

  • 想要有一個快速的 IDE 環境可以使用

    • 公司使用 Windows WSL (openSUSE Leap 15.5) vscode

    • 家裡使用 Macbook Air 15 with M3

    • 因為是不同的 OS 與不同的架構 (Intel and ARM)

  • Cloud Shell 支援 Gemini Code Assist 以及預設裝了開發套件

  • 參加 C 語言的小課程, 希望有個方便的環境開發


先來談談 GCP 的 Cloud Shell

有使用 GCP 的朋友, 非常強烈推薦大力使用目前免費的服務

之前有在網路上看到國外的網友於 2022 年計算 cloud shell 這個免費服務的價值


快速整理一下 現在的規格

  • OS: Ubuntu 24.04 

    • $cat  /etc/*release

  • 4 vCPU - Intel Xeon 2.20 GHz

    • $ cat /proc/cpuinfo | grep CPU

  • 16 G MEM

    • $ free -m

  • 120 GB Persistent-balanced HD for system + 5 GB persistent for home

    • $ lsblk | grep disk

    • $ curl -Ss "http://metadata.google.internal/computeMetadata/v1/instance/disks?recursive=true&alt=text" -H "Metadata-Flavor:  Google" | grep type


每週可以使用 50 小時

觀察方式, 點選 Session information 圖示 -- > Usage Quota


如果用 GCP 計算機計算費用

一個月是 NT$ 1,457.37


剛剛提到有 C 語言小課程, 檢查一下目前的套件狀況


$ gcc  --version


gcc (Ubuntu 13.2.0-23ubuntu4) 13.2.0

Copyright (C) 2023 Free Software Foundation, Inc.

This is free software; see the source for copying conditions.  There is NO

warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


$ g++  --version


g++ (Ubuntu 13.2.0-23ubuntu4) 13.2.0

Copyright (C) 2023 Free Software Foundation, Inc.

This is free software; see the source for copying conditions.  There is NO

warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


$ gdb  --version


GNU gdb (Ubuntu 15.0.50.20240403-0ubuntu1) 15.0.50.20240403-git

Copyright (C) 2024 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law.


如果要編譯 C , Cloud Shell Editor 是使用 Code OSS, 也是可以裝 Extension 的

點選左方的 Extension 圖示, 搜尋 C/C++

我安裝了 C/C++ Runner, C/C++ Config, C/C++ Extension Pack



接下來可以進行測試

建立一個 test.c 的空白檔案

這個時候會看到提示 按 cmd + i 可以問 Gemini Code Assist



在檔案內輸入 // print "hello world" to the console 然後按 Ctrl + Enter 請 Gemini Code Assist 產生程式碼

這個時候就會產生範例程式碼

點選 右上角的 Run 圖示 -- > C/C++ Runner: Run File



如果 Debug File 出現錯誤



開啟 launch.json


將裡面的 type 從 cppdbg 改為 lldb


{

  "version": "0.2.0",

  "configurations": [

    {

      "name": "C/C++ Runner: Debug Session",

      "type": "lldb",

      "request": "launch",

      "args": [],

      "stopAtEntry": false,

      "externalConsole": false,

      "cwd": "/home/sakanamax/lab_c",

      "program": "/home/sakanamax/lab_c/build/Debug/outDebug",

      "MIMode": "gdb",

      "miDebuggerPath": "gdb",

      "setupCommands": [

        {

          "description": "Enable pretty-printing for gdb",

          "text": "-enable-pretty-printing",

          "ignoreFailures": true

        }

      ]

    }

  ]

}


應該就可以執行了


另外也可以同步請 Gemini Code Assist 協助, 例如 加上註解




又前進一步


~ enjoy it


References


沒有留言: