FlagMaker
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    3
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 使用 VSCode 開啟《C++ 教學手冊》範例檔 本書範例原本均是在 Dev-C++ 下建置,預設的程式檔文字編碼都是 Big5 編碼,但是 VSCode 的預設編碼為 UTF-8,因此需要額外的步驟才能正確開啟程式碼檔案。 另外,因為 VSCode 的 C/C++ 延伸模組無法執行位於有中文資料夾路徑的檔案,所以也要注意把範例檔案複製到沒有中文資料夾的路徑中。 ## 開啟範例檔 以下就說明開啟範例檔的步驟: 1. 請確認已經依照〈[使用 VSCode 開發 C/C++ 程式](/imq69VS9QRue4M7r4QgeVg)〉教學文章建置好環境。 3. 下載範例檔並解壓縮: ![圖片](https://hackmd.io/_uploads/H1kXfvMv1e.png) 解開後記得把各章範例檔的資料夾複製到沒有中文的路徑下,本例複製到 "c:/temp/test/samples"。 2. 開啟單章的範例資夾: ![圖片](https://hackmd.io/_uploads/SJZKGPfwJl.png) 執行『**檔案/開啟資料夾**』功能表指令: ![圖片](https://hackmd.io/_uploads/SJnizPfPJe.png) 瀏覽到該章範例資料夾,本例為 CH01,按**選擇資料夾**開啟: ![圖片](https://hackmd.io/_uploads/Bk8eQPzDyx.png) 按**是,我信任作者**。 3. 開啟想要編譯執行的範例檔: ![圖片](https://hackmd.io/_uploads/BJBdmwfw1x.png) 按左邊**檔案總管**後選取要開啟的檔案,本例為 Ch1_1.cpp 檔,你可以看到中文會變成亂碼,請按狀態列中的 **UTF-8** 後按**以編碼重新開啟**: ![圖片](https://hackmd.io/_uploads/B1lx4PfPyx.png) 選取 VSCode 自己猜測出來的 **Traditional Chinese(Big5)** 重新開啟檔案: ![圖片](https://hackmd.io/_uploads/ryfPNwfPyg.png) 你可以看到中文可以正確顯示了,再按下右上角執行鈕旁邊的向下鈕,選取 **Run C/C++ File**: ![圖片](https://hackmd.io/_uploads/Byt24DMD1g.png) 就可以正確編譯並且執行: ![圖片](https://hackmd.io/_uploads/HJ5CNwzDye.png) 切換到**終端機**頁次就可以看到程式的輸出結果了。 :::warning 如果按下執行後看到彈出以下交談窗: ![32](https://hackmd.io/_uploads/S1rP5PzDkg.png) 就表示你的程式檔所在的路徑中有中文名稱,這時其實已經正確編譯完成,只是 VSCode 的 C/C++ 延伸模組無法執行編譯好的程式檔,只要把資料夾移到沒有中文的路徑下重新編譯執行就可以了。 或是你也可以直接在 VSCode 的終端機手動執行,只要切換到**終端機**頁次,輸入 ./Ch1_1" 指令 : ![33](https://hackmd.io/_uploads/B1UdcwzDkg.png) 也可以看到程式正確執行。 ::: :::info 如果不想每次都要重新指定編碼開啟檔案,可以如下操作: ![圖片](https://hackmd.io/_uploads/SJMK1n4w1g.png) 按左下角的**管理**,選**設定**: ![圖片](https://hackmd.io/_uploads/S12yxh4v1g.png) 輸入 "encoding" 搜尋設定項目,勾選 **Files: Auto Guess Encoding**,之後開啟檔案後都會先從檔案內容猜測正確的編碼。 ::: ## 執行程式顯示中文的問題 由於下載的範例檔都是以 Big5 編碼存檔,但是 VSCode 的 C/C++ 延伸模組在執行編譯好的程式時會把環境設定為以 utf-8 編碼,因此在顯示中文的時候也會有問題,底下我們以 Ch04 的 Ch4_1 為例: 1. 依照前面解說的方法在 VSCode 中開啟 Ch04 資料夾,並開啟 Ch4_1.cpp 程式檔: ![圖片](https://hackmd.io/_uploads/Sy19-nEDyx.png) 直接執行後會看到在**終端機**窗格內程式碼的輸出結果為亂碼,這就是因為輸出的中文是以 Big5 編碼,但是 C/C++ 延伸模組把它當成是 utf-8 編碼解譯,所以顯示的就是亂七八糟的結果。 2. 由於我們無法變更 C/C++ 延伸模組的設定,要解決顯示中文的問題,就只能將程式檔重新以 utf-8 編碼存檔。請先按<kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>p</kbd> 開啟 VSCode 的命令選擇區: ![圖片](https://hackmd.io/_uploads/SkPHGhND1x.png) 輸入 "encoding" 搜尋候選**變更檔案的編碼**命令: ![圖片](https://hackmd.io/_uploads/rkvpznVDyx.png) 選**以編碼儲存**命令: ![圖片](https://hackmd.io/_uploads/HyEl7hEDJe.png) 選用 **UTF-8 utf8** 編碼存檔: ![圖片](https://hackmd.io/_uploads/BJ3mQn4D1e.png) 再重新執行程式,就可以看到正確的結果了。 :::info 為了方便大家測試範例,我們另外提供有 utf-8 編碼的範例檔案,可以在[這裡下載]()。 ::: ## 編譯執行 Ch18 多原始檔的程式專案 本書第 18 章介紹了由多個原始檔構成專案的程式,以下我們以 Ch18_18 為例,請先依照之前介紹的方法,在 VSCode 中開啟 Ch18_18 資料夾: 1. 開啟後如果直接執行 Ch18_18.cpp,會顯示如下的錯誤訊息: ![圖片](https://hackmd.io/_uploads/HyXwFM8Pyx.png) 請按**中止** 你可以在底下的終端機窗格看到錯誤訊息: ![圖片](https://hackmd.io/_uploads/HykpUmUPJe.png) 錯誤訊息表示它不知道 CWnd::show(),這是因為我們編譯的是 Ch18_18.cpp,但 CWnd::show() 是定義在 cwin.cpp 中,並沒有編譯,所以發生錯誤。 2. 修改設定檔編譯資料夾下的所有檔案,請從左檔案總管窗格中開啟 .vscode\taks.json 檔: ![圖片](https://hackmd.io/_uploads/B12GeNIvJx.png) 把圖中的 `${file}` 改成 `${workspaceFolder}/ *.cpp`: ![圖片](https://hackmd.io/_uploads/SkZzfELwkg.png) 確認修改結果如上後儲存檔案。 3. 重新回到包含有專案 main() 函數的 Ch18_18.cpp 檔案: ![圖片](https://hackmd.io/_uploads/ryoBGELDJl.png) 執行程式: ![圖片](https://hackmd.io/_uploads/BJT8zVLPke.png) 就可以看到執行結果。 只要依照以上步驟,就可以編譯執行 Ch18 後續各個需要多個原始檔構成專案的程式了。 :::info 如果你都只會透過 C/C++ 延伸模組執行程式,不需要在終端機執行編譯好的程式,可以略過以下內容。 ::: ## 在終端機單獨執行程式顯示中文的問題 我們雖然解決了在 C/C++ 延伸模組執行時顯示中文的問題,不過如果接著在終端機執行的話,反而顯示結果變成亂碼了,例如: ![圖片](https://hackmd.io/_uploads/SkzLEhNv1e.png) 你可以看到剛剛明明正常顯示的中文現在卻變成亂碼,這是因為 VSCode 預設開啟的終端機會使用 PowerShell 作為指令層,在 Windows 繁體中文版上預設輸入與輸出文字都採用 Big5 編碼,因此會把程式送出的 utf-8 編碼文字當成 Big5 編碼解譯導致錯誤,顯示成亂七八糟的結果了。 要解決這個問題,必須執行額外的指令告知 PowerShell 程式送出的文字編碼是 utf-8: ![圖片](https://hackmd.io/_uploads/Skc5IhEwJe.png) 請先執行 `[Console]::OutputEncoding = [text.encoding]::UTF8` 指令變更設定,再重新從終端機直接執行程式就可以看到正確的中文顯示了。 如果不是在 VSCode 開啟的終端機中執行程式,而是透過**命令提示字元**,也會遇到相同的問題: ![圖片](https://hackmd.io/_uploads/B1DSwhVv1g.png) 在**命令提示字元**中必須下達 `chcp 65001` 更改字碼頁,其中 `65001` 是 utf-8 的字碼頁識別碼,設定後重新執行程式就會看到正確的中文顯示了: ![圖片](https://hackmd.io/_uploads/Syxxd3Vwyx.png) :::info 剛剛的設定都會在重新開啟終端機或是命令提示字元後恢復原始的設定值。 ::: ## 在終端機單獨執行程式輸入中文的問題 如果程式需要輸入中文,透過 C/C++ 延伸模組執行因為會將輸入與輸出都設為使用 utf-8,兩者一致,因此把輸入的中文印出來,並不會有問題,以下以 Ch08 的 Ch8_12.cpp 為例: ![圖片](https://hackmd.io/_uploads/ByefyRVwJg.png) 不過如果接著在終端機窗格中單獨執行,就會發生問題: ![圖片](https://hackmd.io/_uploads/ByCl-AVD1g.png) 這是因為 C/C++ 延伸模組執行完程式後,輸入文字的編碼會被改成 utf-8,而輸出文字的編碼是前一小節提到的 Big5,導致兩邊的文字編碼不一致。為了解決這個問題,最簡易的方法就是: 1. 程式碼本身一樣改成 utf-8 編碼存檔。 2. 把終端機本身輸入與輸出的編碼也都設為 utf-8 編碼,這樣包含程式碼原本就需要顯示的文字在內,所有文字處理都統一使用 utf-8,就不會有問題了。 你可透過上圖看到設定統一的 utf-8 編碼後,都可以正常運作了。 如果是在**命令提示字元**內,只要下達 `chcp 65001` 指令,就可以統一輸入與輸出文字的編碼為 utf-8。

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully