Eric W.
  • NEW!
    NEW!  Connect Ideas Across Notes
    Save time and share insights. With Paragraph Citation, you can quote others’ work with source info built in. If someone cites your note, you’ll see a card showing where it’s used—bringing notes closer together.
    Got it
      • Create new note
      • Create a note from template
        • 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
          • Only me
          • Signed-in users
          • Everyone
          Only me Signed-in users Everyone
        • Write
          • Only me
          • Signed-in users
          • Everyone
          Only me 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 No publishing access yet

        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.

        Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

        Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

        Explore these features while you wait
        Complete general settings
        Bookmark and like published notes
        Write a few more notes
        Complete general settings
        Write a few more notes
        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 New
      • Engagement control
      • Make a copy
      • Transfer ownership
      • Delete this note
      • Save as template
      • Insert from template
      • Import from
        • Dropbox
        • Google Drive
        • Gist
        • Clipboard
      • Export to
        • Dropbox
        • Google Drive
        • Gist
      • Download
        • Markdown
        • HTML
        • Raw HTML
    Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
    Create Create new note Create a note from template
    Menu
    Options
    Engagement control Make a copy 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
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    Write
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me 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 No publishing access yet

    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.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    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
    1
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- title: 'C++編譯環境建構(in VSCode)' disqus: hackmd --- C++編譯環境建構(in VSCode) === ![downloads](https://img.shields.io/github/downloads/atom/atom/total.svg) ![build](https://img.shields.io/appveyor/ci/:user/:repo.svg) ![chat](https://img.shields.io/discord/:serverId.svg) ## 大綱 [TOC] ## 前置作業 VScode是一個具擴充能力的"文字編輯器"(本身不帶有編譯器),要能夠編譯程式語言通常需要透過安裝"插件"來獲得能力。 首先,在建構環境前你需要: 1. 安裝 [Visual Studio Code](https://code.visualstudio.com/download) 2. 安裝 VScode的C++插件 ![C++Extension](https://i.imgur.com/YiHsidx.png) 3. 下載 [Mingw-w64](http://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/Personal%20Builds/mingw-builds/installer/mingw-w64-install.exe/download)並安裝 ::: warning 注意 : - Mingw安裝的路徑請不要含有"**空格**"以及"**中文**",以免產生問題。(不要使用預設的路徑,那有~~他媽的~~空格) - 建議裝在 "C:\\",本教學以此路徑作為範例 ::: 4. 將Mingw內的"bin"目錄的路徑加到Windows環境變數PATH裡面: 1.按下 Win+Pause 2.點選 進階系統設定 3.點選 環境變數 ![環境變數](https://i.imgur.com/bArpbGu.png) 4.選取 系統變數 的 Path,點 編輯 5.點選 新增 ,輸入 Mingw的bin目錄的路徑![](https://i.imgur.com/1gwr0SC.png) 5. 測試:在cmd("命令提示字元")輸入 : ``` g++ --version``` 若執行成功,則環境變數設定成功。 建立工作目區 --- 工作區會除了可以設定工作目錄外,還能保存針對工作區的個別設定,對於配置不同的程式語言編譯環境十分有幫助。 本文也是以以工作區來設定建構環境的。 首先,你需要選定一個資料夾(**資料夾路徑請不要有空格以及中文**)作為我們編譯C/C++所使用的辜作區, ![選擇資料夾](https://i.imgur.com/MXh1buV.png) 在VSCode開啟資料夾後,點選"另存工作區為",將工作區檔存放於工作資料夾內, ![](https://i.imgur.com/Mc2X6wz.png) 編輯設定檔 --- *產生設定檔目錄(.vscode)和設定檔* 此段落所有操作都是在工作區進行。 C++插件的擴充套件是透過[JSON](https://zh.wikipedia.org/wiki/JSON)檔案來設定的(剛剛完成的工作區也是)。 首先我們先處理插件的JSON設定檔--```c_cpp_properties.json```,按下```Ctrl+Shift+P```叫出選單,找到```C/C++:Edit Configurations (UI)```,進入之後你會看到一個圖像化的介面如下![C/C++:Edit Configurations (UI)](https://i.imgur.com/pcauXdu.png),將"編輯器路徑"改成```C:\Mingw-w64\bin\g++.exe```,並將IntelliSense模式的設定改為```gcc-x64```,存檔後關閉。 接著我們新增一份.cpp檔案,接著按下F5, ![new a helloworld.cpp](https://i.imgur.com/ai4qvQs.png) 會跳出一個選單,選擇第一個選項"C++ (GDB/LLDB)", ![F5選單](https://i.imgur.com/fzuhvrc.png) 接著選"g++.exe ...", ![F5選單2](https://i.imgur.com/NrJa05z.png) 他就會把```launch.json``` 、 ```tasks.json```都改建構好了喔! *無效則,手動建立json任務檔 再來是```launch.json```設定檔,它會決定當你按下```F5```(或是左邊選單的"偵錯")會執行的"任務",內容應設定為: ```json { "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${fileDirname}/${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "C:/Mingw-w64/bin/gdb.exe", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "build" ] } ``` 最後是```tasks.json```設定檔,他描述了一個要預先執行的任務內容,並交由launch.json執行("preLaunchTask": "build"),其內容應為: ```json { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "build", "command": "C:/Mingw-w64/bin/g++.exe", "args": [ "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}.exe" ], "group": { "kind": "build", "isDefault": true } } ] } ``` *變數說明 : ${fileDirname}, ${fileBasenameNoExtension}, ${workspaceFolder}, etc.. 操作說明 --- 每當要編譯.cpp檔案時,要先開啟工作區,接著開啟檔案按下F5,就會編譯並並運行囉。 而程式執行後輸入輸出的結果會在下方"終端機"的標籤頁下。 :::info 如果想要有像是Devc++、CodeBlocks一樣有一個外部的cmd來輸入輸出,你需要修改```launch.json```設定中的 "externalConsole" : ![externalConsole](https://i.imgur.com/dh4jqoA.png) 將 "externalConsole" 的設定改為```true```。 ::: ## 原理說明 ## 參考資料 https://code.visualstudio.com/docs/cpp/config-mingw --微軟寫的非常完整 ###### tags: `tutorial`, `C/C++`, `programming`

    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
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    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