2017年6月24日 星期六

在Visual Studio Code寫C/C++ (1) - 第一個執行檔HelloWorld

2017/06/24 10:28~12:25

Visual Studio Code (VSCode) 是個單純的編輯器,但有著非常多得外掛,安裝之後你可以用它來開發Java, PHP, JavaScript 和 Golane等等許多程式語言,也有很多很棒的編輯器工具可以使用。

自從我換從Windows跳槽到Mac之後,一直找不到像Notepad++好用的編輯器,最近因為覺得用XCode開發C/C++程式很不友善,而且XCode只有Mac能用,於事一直在找免費且跨平台的IDE,找到了VSCode,意外發現Notepad++好用的功能他都有,包含我最愛用的編輯器功能『利用關鍵字過濾出此檔案中包含關鍵字的所有行數文字』,既然有這個功能,就沒有理由不用它了啊~!

這篇網誌會用VSCode, C/C++ plugin 還有CMake完成一個helloWorld的執行檔 。


1. 建立專案







2. 設定專案為C/C++
新增兩個檔案:

  • CMakeLists.txt
  • hello-vscode.cpp







CMakeLists.txt內容如下:
cmake_minimum_required(VERSION 3.0)
project(hello-vscode)
set(SOURCE hello-vscode.cpp)
add_executable(${PROJECT_NAME} ${SOURCE})
hello-vscode.cpp內容如下:
#include <iostream>
int main(int argc, const char * argv[]) {
    std::cout << "hello Visual Studio Code! :)" << '\n'; 
    return 0;
}


3. 產生c_cpp_properties.json
  • 在編輯器按下F1,會出現Command列表
  • 輸入"C/Cpp: Edit Configurations"
  • 按下Enter


c_cpp_properties.json內容如下:
{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceRoot}",
                "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1",
                "/usr/local/include",
                "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/8.1.0/include",
                "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include",
                "/usr/include"
            ],
            "defines": [],
            "browse": {
                "path": [
                    "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1",
                    "/usr/local/include",
                    "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/8.1.0/include",
                    "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include",
                    "/usr/include"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        },
        {
            "name": "Linux",
            "includePath": [
                "${workspaceRoot}",
                "/usr/include",
                "/usr/local/include"
            ],
            "defines": [],
            "browse": {
                "path": [
                    "/usr/include",
                    "/usr/local/include"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        },
        {
            "name": "Win32",
            "includePath": [
                "${workspaceRoot}",
                "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE"
            ],
            "browse": {
                "path": [
                    "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include/*"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        }
    ]
}

4. 產生CMake和Make的Tasks設定檔tasks.json
  • 按下F1打開Command表
  • 輸入"Tasks: Configure Task Runner",
  • 選擇Task為"Others"



修改tasks.json內容如下:
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "command": "sh",
    "isShellCommand": true,
    "args": ["-c"],
    "showOutput": "always",
    "suppressTaskName": true,
    "options": {
        "cwd": "${workspaceRoot}/build"
    },
    "tasks": [
        {
            "taskName": "cmake",
            "args": ["cmake -G 'Unix Makefiles' -DCMAKE_BUILD_TYPE=Debug .."]     
        },
        {
            "taskName": "make",
            "args": ["make -j 8"],
            "isBuildCommand": true            
        }
    ]
}
其中cwd代表Current Working Directory,${workspaceRoot}/build則代表會把產生出來的檔案放在此目錄中。但目前沒有build的目錄,我們必須手動建立build資料夾。





5. 執行CMake

  • 案F1打開Command列表
  • 輸入"Run Task",選擇"Tasks: Run Task"
  • 選擇CMake



6. 產生執行檔
  • 案F1打開Command列表
  • 輸入"Run Build Task"
  • 進入build資料夾
  • 在Terminal中輸入"./hello-vscode"執行程式





7. 使用LLDB Debug
  • 點選debug按鈕
  • 點選齒輪
  • 選擇"C++ (GDB/LLDB)",產生launch.json
  • 將launch.json中的program改為"${workspaceRoot}/build/hello-vscode"
  • 將launch.json中的cwd改為"${workspaceRoot}"
  • 在hello-vscode.cpp中設定斷點
  • 點選Start Debugging開始debug






滑鼠點選行號左邊,即可產生程式斷點 



點選紅色框框處或按下快捷鍵F5開始除錯



最後launch.json內容會長這樣
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(lldb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceRoot}/build/hello-vscode",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceRoot}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "lldb"
        }
    ]
}

。。。


終於完成在VSCode中的第一個C++程式啦!


沒有留言:

張貼留言