在 Visual Studio 中建置和清除專案與方案Build and clean projects and solutions in Visual Studio

您可以使用本主題中的程序來建置、重建或清除所有或部分專案,或方案中的專案項目。By using the procedures in this topic, you can build, rebuild, or clean all or some of the projects or project items in a solution. 如需逐步教學課程,請參閱逐步解說:建置應用程式For a step-by-step tutorial, see Walkthrough: Building an application.

Note

本主題適用於 Windows 上的 Visual Studio。This topic applies to Visual Studio on Windows. 針對 Visual Studio for Mac,請參閱在 Visual Studio for Mac 中建置和清除專案和方案For Visual Studio for Mac, see Build and clean projects and solutions in Visual Studio for Mac.

Note

您 Visual Studio 版本中的 UI 可能不同於本主題所述,視您目前使用的設定而定。The UI in your edition of Visual Studio might differ from what this topic describes, depending on your active settings. 若要變更您的設定 (例如變更為 [一般] 或 [Visual C++] 設定),請選擇 [工具] > [匯入和匯出設定] ,然後選擇 [重設所有設定] 。To change your settings, for example to General or Visual C++ settings, choose Tools > Import and Export Settings, and then choose Reset all settings.

建置、重建或清除整個方案To build, rebuild, or clean an entire solution

  1. 在方案總管 中,選擇或開啟方案。In Solution Explorer, choose or open the solution.

  2. 在功能表列上選擇 [建置] ,然後選擇下列其中一個命令︰On the menu bar, choose Build, and then choose one of the following commands:

    • 選擇 [建置] 或 [建置方案] ,只編譯自從最近建置後已變更的那些專案檔案和元件。Choose Build or Build Solution to compile only those project files and components that have changed since the most recent build.

      Note

      當方案包含多個專案時,[建置] 命令會變成 [建置方案] 。The Build command becomes Build Solution when a solution includes more than one project.

    • 選擇 [重建方案] 以「清除」方案,然後建置所有專案檔和元件。Choose Rebuild Solution to "clean" the solution and then build all project files and components.

    • 選擇 [清除方案] 以刪除任何中繼和輸出檔。Choose Clean Solution to delete any intermediate and output files. 只留下專案和元件檔案,即可建立新的中繼檔和輸出檔執行個體。With only the project and component files left, new instances of the intermediate and output files can then be built.

建立或重建單一專案To build or rebuild a single project

  1. 在方案總管 中,選擇或開啟專案。In Solution Explorer, choose or open the project.

  2. 在功能表列上選擇 [建置] ,然後選擇 [建置 ProjectName] 或 [重建 ProjectName] 。On the menu bar, choose Build, and then choose either Build ProjectName or Rebuild ProjectName.

    • 選擇 [建置 ProjectName ] ,只編譯自從最近建置後已變更的那些專案元件。Choose Build ProjectName to build only those project components that have changed since the most recent build.

    • 選擇 [重建 ProjectName] 以「清除」專案,然後建置專案檔和所有專案元件。Choose Rebuild ProjectName to "clean" the project and then build the project files and all project components.

僅建置啟始專案和其相依性To build only the startup project and its dependencies

  1. 在功能表列上選擇 [工具] > [選項] 。On the menu bar, choose Tools > Options.

  2. 在 [選項] 對話方塊方塊中,展開 [專案和方案] 節點,然後選擇 [建置並執行] 頁面。In the Options dialog box, expand the Projects and Solutions node, and then choose the Build and Run page.

    [建置並執行] > [專案和方案] > [選項] 對話方塊隨即開啟。The Build and Run > Projects and Solutions > Options dialog box opens.

  3. 選取 [僅在執行時建置啟始專案和相依性] 核取方塊。Select the Only build startup projects and dependencies on Run check box.

    如果選取此核取方塊,當您執行下列步驟之一時,只會建置目前的啟始專案和其相依性︰When this check box is selected, only the current startup project and its dependencies are built when you perform either of the following steps:

    • 在功能表列上,選擇 [偵錯] > [開始] (F5)。On the menu bar, choose Debug > Start (F5).

    • 在功能表列上選擇 [建置] > [建置方案] (Ctrl+Shift+B)。On the menu bar, choose Build > Build Solution (Ctrl+Shift+B).

    如果清除此核取方塊,當您執行其中一個上述命令時,會建置所有專案、其相依性及方案檔。When this check box is cleared, all projects, their dependencies, and the solution files are built when you run either of the preceding commands. 根據預設,會清除此核取方塊。By default, this check box is cleared.

只建置選取的 Visual C++ 專案To build only the selected Visual C++ project

選擇 Visual C++Visual C++ 專案,然後在功能表列上選擇 [建置] > [僅限專案] ,以及下列其中一個命令︰Choose a Visual C++Visual C++ project, and then, on the menu bar, choose Build > Project Only, and one of the following commands:

  • 僅限建置 <專案名稱> Build Only ProjectName

  • 僅限重建 <專案名稱> Rebuild Only ProjectName

  • 僅清除 <專案名稱> Clean Only ProjectName

  • 僅連結 <專案名稱> Link Only ProjectName

這些命令只適用於您選擇的 Visual C++Visual C++ 專案,而不建置、重建、清除或連結任何專案相依性或方案檔。These commands apply only to the Visual C++Visual C++ project that you chose, without building, rebuilding, cleaning, or linking any project dependencies or solution files. 根據您的 Visual StudioVisual Studio 版本,[僅限專案] 子功能表可能包含更多命令。Depending on your version of Visual StudioVisual Studio, the Project Only submenu might contain more commands.

編譯多個 C++ 專案項目To compile multiple C++ project items

在方案總管中 ,選擇具有可編譯動作的多個檔案、開啟這些檔案之一的捷徑功能表,然後選擇 [編譯] 。In Solution Explorer, choose multiple files that have can be compiled actions, open the shortcut menu for one of those files, and then choose Compile.

如果檔案具有相依性,就會將檔案依照相依性順序編譯。If the files have dependencies, the files will be compiled in dependency order. 如果檔案需要先行編譯標頭檔,但在您進行編譯時無法使用,則編譯作業會失敗。The compile operation will fail if the files require a precompiled header that isn't available when you compile. 編譯作業會使用目前的作用中方案組態。The compile operation uses the current active solution configuration.

停止建置To stop a build

執行下列任一步驟:Perform either of the following steps:

  • 在功能表列中,選取 [建置] > [取消] 。On the menu bar, select Build > Cancel.

  • 按下 Ctrl+BreakPress Ctrl+Break.

另請參閱See also