前往
大廳
主題

在CentOS 7上使用Visual Studio Code開發標準C語言程式

Yang | 2022-12-04 10:29:35 | 巴幣 0 | 人氣 326

接續之前寫的,在Windows上使用Visual Studio Code開發標準C語言程式,在此紀錄CentOS 7上的安裝過程,以VSCode 1.73當範例

Step0:先建立帶桌面環境(譬如GNOME)的CentOS 7,用一般帳號登入,不需要用root帳號

Step1:影片00:01,先把帳號加入sudoers,影片00:40,sudoers應該是唯讀,改好下wq!即可存檔
su root
vi /etc/sudoers
[username] ALL=(ALL) ALL
exit
cat /etc/sudoers | grep ALL

Step2:影片00:52,安裝程式開發所需套件環境,指令yum groupinstall 'Development Tools'

Step3:影片01:23,安裝VSCode,這裡是抄官方範例(https://code.visualstudio.com/docs/setup/linux)
(這裡用了root帳號是不必要的,一般帳號即可)

Step4:影片03:12,(可能要重開機)指令code,啟動VSCode,安裝一些方便開發的輔助外掛工具,搜尋c++、CMake、Makefile即可看到
Step5:影片07:49,在專案資料夾內建立.vscode資料夾,預設是個隱藏資料夾,檔案總管看不到
Step6:影片08:23,按F1,找到並設定C/C++: Edit Configurations (UI),編譯器選gcc,產生c_cpp_properties.json
Step7:影片10:51,建立和設定tasks.json
Step8:影片11:02,建立和設定launch.json
Step9:影片11:22,按Ctrl+Shift+B,編譯專案,成功會在專案資料夾內產生執行檔
Step10:影片11:53,按F5,開時執行與偵錯程式碼

設定好c_cpp_properties.json,tasks.json,launch.json,即可開始開發/編譯/偵錯C語言寫的程式

除非是呼叫只有特定作業系統支援的API,否則一般的程式碼,應該不用修改,就能在Windows和Linux上編譯

但Linux版本眾多,在CentOS 7上產生的執行檔,不確定能否搬移到其他CentOS或Linux上執行,待測試

VSCode和Visual Studio操作上可能不同,譬如F5僅代表偵錯,而不是編譯+偵錯,因此要養成(Ctrl+Shift+B編譯)+(F5偵錯)的習慣

三個json範例如下:
c_cpp_properties.json:
{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++11",
            "intelliSenseMode": "linux-gcc-x64",
            "configurationProvider": "ms-vscode.makefile-tools"
        }
    ],
    "version": 4
}

tasks.json:
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-std=c++11",
                "-pthread",
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ]
}

launch.json:
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C/C++: g++ build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb",
            "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
            ],
            "preLaunchTask": "C/C++: g++ build active file"
        }
    ]
}
送禮物贊助創作者 !
0
留言

創作回應

更多創作