Visual Studio 中的 C++ 使用者入門Get started with C++ in Visual Studio

完成這個快速入門,以熟悉許多可在 C++ 中使用 Visual Studio 開發應用程式時使用的工具和對話方塊。Complete this quickstart to become familiar with many of the tools and dialog boxes that you can use when you develop applications in C++ with Visual Studio. 建立 "Hello, World" 型主控台應用程式,同時深入了解如何使用整合式開發環境 (IDE)。Create a "Hello, World"-style console application while you learn more about working in the integrated development environment (IDE).

必要條件Prerequisites

您不需要熟悉 C++ 即可完成此快速入門,但應該熟悉一些一般程式設計和偵錯概念。You don't need to be familiar with C++ to complete this quickstart, but you should be familiar with some general programming and debugging concepts. Visual Studio 文件不會教導您如何在 C++ 中進行程式設計。The Visual Studio documentation doesn't teach you how to program in C++. 不錯的 C++ 學習資源指南是 ISO C++ 網站上的使用者入門頁面。A good guide to C++ learning resources is the Get started page on the ISO C++ website.

若要跟著做,您需要一份 Visual Studio 2017 複本,其中已安裝使用 C++ 的桌面開發工作負載。To follow along, you need a copy of Visual Studio 2017 with the Desktop development with C++ workload installed. 如需安裝的快速指南,請參閱在 Visual Studio 中安裝 C++ 支援For a fast guide to installation, see Install C++ support in Visual Studio.

若要跟著做,您需要一份 Visual Studio 2019 複本,其中已安裝使用 C++ 的桌面開發工作負載。To follow along, you need a copy of Visual Studio 2019 with the Desktop development with C++ workload installed. 如需安裝的快速指南,請參閱在 Visual Studio 中安裝 C++ 支援For a fast guide to installation, see Install C++ support in Visual Studio.

建立主控台應用程式Create a console app

如果尚未執行,請開啟 Visual Studio。If it's not running yet, open Visual Studio.

已套用 Visual C++ 設定的 IDE

開啟 Visual Studio 之後,您會看到 IDE 的三個基本部分:工具視窗、功能表和工具列,以及主視窗空間。After you open Visual Studio, you can see the three basic parts of the IDE: tool windows, menus and toolbars, and the main window space. 工具視窗停駐在應用程式視窗的左側和右側。Tool windows are docked on the left and right sides of the app window. [快速啟動] 方塊、功能表列和標準工具列位於頂端。The Quick Launch box, the menu bar, and the standard toolbar are found at the top. 視窗中央包含 [起始頁]。The center of the window contains the Start Page. 當您開啟方案或專案時,編輯器和設計工具就會出現在此空間中。When you open a solution or project, editors and designers appear in this space. 開發應用程式時,您大部分的時間都花在此中央區域。When you develop an app, most of your time is spent in this central area.

開啟 Visual Studio 後,會先出現 [開始] 視窗。After you open Visual Studio, the start window appears first. 選取 [不使用程式碼繼續] 以開啟開發環境。Select Continue without code to open the development environment.

您會看到 IDE 的三個基本部分:工具視窗、功能表和工具列,以及主視窗空間。You'll see the three basic parts of the IDE: tool windows, menus and toolbars, and the main window space. 工具視窗停駐在應用程式視窗的左側和右側。Tool windows are docked on the left and right sides of the app window. 搜尋方塊、功能表列和標準工具列位於頂端。The search box, menu bar, and standard toolbar are found at the top. 當您載入方案或專案時,編輯器和設計工具會出現在應用程式視窗的中央區域。When you load a solution or project, editors and designers appear in the central space of the application window. 在開發應用程式時,您大部分時間都會在此中央區域工作。When you develop an application, you'll spend most of your time in this central area.

Visual Studio 會使用「專案」來組織應用程式的程式碼,並使用「解決方案」來組織專案。Visual Studio uses projects to organize the code for an app, and solutions to organize your projects. 專案包含用來建置您應用程式的所有選項、組態和規則。A project contains all the options, configurations, and rules used to build your apps. 它也會管理所有專案之檔案與任何外部檔案間的關聯性。It also manages the relationship between all the project's files and any external files. 若要建立您的應用程式,請先建立新的專案和解決方案。To create your app, first, you create a new project and solution.

建立主控台應用程式專案To create a console app project

  1. 在功能表列上,選擇 [檔案] > [新增] > [專案],開啟 [新增專案] 對話方塊。On the menu bar, choose File > New > Project to open the New Project dialog box.

    在功能表列上,選擇 [檔案] > [新增] > [專案]

  2. 在 [新增專案] 對話方塊中,選取 [已安裝] > [Visual C++] (如果尚未選取)。In the New Project dialog, select Installed > Visual C++ if it isn't selected already. 在中央窗格中,選取 [Windows 主控台應用程式] 範本。In the center pane, select the Windows Console Application template. 在 [名稱] 編輯方塊中,輸入 HelloAppIn the Name edit box, enter HelloApp.

    使用 [新增專案] 對話方塊來建立應用程式專案

    根據已安裝的 Visual Studio 工作負載和元件,您的對話方塊可能會有不同的選擇。Your dialog box may have different choices, depending on the Visual Studio workloads and components you've installed. 如果看不到 Visual C++ 專案範本,則需要重新執行 Visual Studio 安裝程式,並安裝使用 C++ 的桌面開發工作負載。If you don't see Visual C++ project templates, you need to run the Visual Studio installer again and install the Desktop development with C++ workload. 您可以直接從 [新增專案] 對話方塊執行此作業。You can do this directly from the New Project dialog. 若要啟動安裝程式,請選擇對話方塊上的 [開啟 Visual Studio 安裝程式] 連結。To launch the installer, choose the Open Visual Studio Installer link on the dialog.

  3. 選擇 [確定] 按鈕,以建立應用程式專案和解決方案。Choose the OK button to create your app project and solution.

    這樣會建立 HelloApp 專案和解決方案以及 Windows 主控台應用程式的基本檔案,並自動載入 方案總管The HelloApp project and solution, with the basic files for a Windows console app, are created and automatically loaded into Solution Explorer. HelloApp.cpp 檔案會在程式碼編輯器中開啟。The HelloApp.cpp file is opened in the code editor. 這些項目會在方案總管中出現:These items appear in Solution Explorer:

    方案在方案總管中的所有檔案

將程式碼新增至應用程式Add code to the app

接下來,新增程式碼以在主控台視窗中顯示 "Hello" 文字。Next, add code to display the word "Hello" in the console window.

在編輯器中編輯程式碼To edit code in the editor

  1. 在 HelloApp.cpp 檔案的 return 0; 行之前輸入空白行,然後輸入下列程式碼:In the HelloApp.cpp file, enter a blank line before the line return 0; and then enter this code:

    cout << "Hello\n";
    

    cout底下會出現紅色曲線。A red squiggly line appears under cout. 如果您將滑鼠指標停留在它上方,則會出現錯誤訊息。If you hover the pointer over it, an error message appears.

    cout 的錯誤文字

    錯誤訊息也會出現在 [錯誤清單] 視窗中。The error message also appears in the Error List window. 您可以在功能表列上選擇 [檢視] > [錯誤清單] 來顯示此視窗。You can display this window by choosing View > Error List on the menu bar.

    [錯誤清單] 視窗中的錯誤

    您的程式碼遺失 std::cout 的宣告,其位在 <iostream> 標頭檔中。Your code is missing a declaration for std::cout, which is found in the <iostream> header file.

  2. 若要包含 iostream 標頭,請在 #include "stdafx.h" 後面輸入下列程式碼:To include the iostream header, enter this code after #include "stdafx.h":

    #include <iostream>
    using namespace std;
    

    您可能會注意到在您輸入程式碼時會出現一個方塊。You probably noticed that a box appeared as you entered code. 此方塊包含您所輸入字元的自動完成建議。This box contains auto-completion suggestions for the characters that you enter. 它是 C++ IntelliSense 的一部分,可提供編碼提示,包含類別或介面成員和參數資訊。It's part of C++ IntelliSense, which provides coding prompts, including class or interface members and parameter information. 您也可以使用程式碼片段,也就是預先定義的程式碼區塊。You can also use code snippets, which are pre-defined blocks of code. 如需詳細資訊,請參閱使用 IntelliSense程式碼片段For more information, see Using IntelliSense and Code snippets.

    編輯器中的已修正程式碼

    當您修正錯誤時, cout 底下的紅色曲線就會消失。The red squiggly line under cout disappears when you fix the error.

  3. 若要將變更儲存至檔案,請按 Ctrl+STo save the changes to the file, press Ctrl+S.

建置應用程式Build the app

建置程式碼十分輕鬆。It's easy to build your code. 在功能表列上,選擇 [建置] > [建置解決方案]。On the menu bar, choose Build > Build Solution. Visual Studio 會建置 HelloApp 解決方案,並在 [輸出] 視窗中報告進度。Visual Studio builds the HelloApp solution, and reports progress in the Output window.

建置 HelloApp 解決方案

偵錯和測試應用程式Debug and test the app

您可以偵錯 HelloApp,看看 "Hello" 這個字是否出現在主控台視窗中。You can debug HelloApp to see whether the word "Hello" appears in the console window.

偵錯應用程式To debug the app

若要啟動偵錯工具,請選擇功能表列上的 [偵錯] > [開始偵錯]。To start the debugger, choose Debug > Start Debugging on the menu bar.

[偵錯] 功能表上的 [開始偵錯] 命令

偵錯工具會啟動並執行程式碼。The debugger starts and runs the code. 主控台視窗 (外觀類似命令提示字元的另一個視窗) 會出現幾秒鐘,並在偵錯工具停止執行時快速關閉。The console window (a separate window that looks like a command prompt) appears for a few seconds but closes quickly when the debugger stops running. 若要查看文字,則必須設定中斷點以停止程式執行。To see the text, you need to set a breakpoint to stop program execution.

若要加入中斷點To add a breakpoint

  1. 在編輯器中,將資料指標放在 return 0; 行。In the editor, put the cursor on the line return 0;. 在功能表列上,選擇 [偵錯] > [切換中斷點]。On the menu bar, choose Debug > Toggle Breakpoint. 您也可以按一下左邊界來設定中斷點。You can also click in the left margin to set a breakpoint.

    [偵錯] 功能表上的 [切換中斷點] 命令

    在編輯器視窗最左緣、程式碼行的旁邊會出現一個紅色圓圈。A red circle appears next to the line of code in the far left margin of the editor window.

    視窗邊界中指出的中斷點

  2. 若要開始偵錯,請按 F5To start debugging, press F5.

    偵錯工具隨即啟動,而主控台視窗會出現並顯示 Hello這個字。The debugger starts, and a console window appears showing the word Hello.

    主控台視窗中的 Hello 文字

  3. 若要停止偵錯,請按 Shift+F5To stop debugging, press Shift+F5.

如需主控台專案偵錯的詳細資訊,請參閱主控台專案For more information about console project debugging, see Console projects.

建置應用程式的發行版本Build a release version of the app

既然已經驗證應用程式的運作一切正常,您就可以準備其發行組建。Now that you've verified that everything works, you can prepare a release build of the application. 發行組建會保留偵錯資訊,並使用編譯器最佳化選項來建立較小且更快速的程式碼。Release builds leave out the debugging information, and use compiler optimization options to create smaller, faster code.

清除方案檔案和建置發行版本To clean the solution files and build a release version

  1. 在功能表列上,選擇 [建置] > [清除解決方案],刪除在上一個建置期間建立的中繼檔和輸出檔。On the menu bar, choose Build > Clean Solution to delete intermediate files and output files that were created during previous builds.

    [建置] 功能表上的 [清除方案] 命令

  2. 若要將 HelloApp 的方案組態從 [偵錯] 變更為 [發行],請在工具列中選取 [解決方案組態] 控制項的下拉式清單,然後選擇 [發行]。To change the solution configuration for HelloApp from Debug to Release, in the toolbar, select the dropdown on the Solution Configurations control and then choose Release.

    建置應用程式的發行版本

  3. 建置方案。Build the solution. 在功能表列上,選擇 [建置] > [建置解決方案]。On the menu bar, choose Build > Build Solution.

此建置完成時,您已建立可在任何命令提示字元視窗中複製和執行的應用程式。When this build completes, you've created an app that you can copy and run in any command prompt window. 它可能不會執行太多作業,但為更高作業的閘道。It may not do much, but it's the gateway to greater things.

恭喜您完成此快速入門!Congratulations on completing this quickstart!

另請參閱See also