逐步解說:在命令列上編譯原生 C++ 程式Walkthrough: Compiling a Native C++ Program on the Command Line

04/02/2020

本文內容

Visual Studio 包含命令列 C 和 c + + 編譯器。Visual Studio includes a command-line C and C++ compiler. 您可以使用它來建立從基本主控台應用程式到通用 Windows 平臺應用程式、桌面應用程式、設備磁碟機和 .NET 元件的所有專案。You can use it to create everything from basic console apps to Universal Windows Platform apps, Desktop apps, device drivers, and .NET components.

在本逐步解說中,您會使用文字編輯器來建立基本的 "Hello,World" 樣式 c + + 程式,然後在命令列上進行編譯。In this walkthrough, you create a basic, "Hello, World"-style C++ program by using a text editor, and then compile it on the command line.

在此逐步解說中,您可以使用自己的 c + + 程式,而不是輸入所顯示的程式。In this walkthrough, you can use your own C++ program instead of typing the one that's shown. 或者,您可以使用其他說明文章中的 c + + 程式碼範例。Or, you can use a C++ code sample from another help article.

先決條件Prerequisites

若要完成此逐步解說,您必須安裝 Visual Studio 和選用的桌面開發與 c + + 工作負載,或是 Visual Studio 的命令列組建工具。To complete this walkthrough, you must have installed either Visual Studio and the optional Desktop development with C++ workload, or the command-line Build Tools for Visual Studio.

Visual Studio 是整合式開發環境(IDE)。Visual Studio is an integrated development environment (IDE). 它支援許多語言和平臺的功能完整編輯器、資源管理員、偵錯工具和編譯器。It supports a full-featured editor, resource managers, debuggers, and compilers for many languages and platforms. 可用的版本包括免費的 Visual Studio Community edition,而且全部都可以支援 C 和 c + + 開發。Versions available include the free Visual Studio Community edition, and all can support C and C++ development. 如需有關如何下載及安裝 Visual Studio 的詳細資訊,請參閱在 Visual Studio 中安裝 c + + 支援。For information on how to download and install Visual Studio, see Install C++ support in Visual Studio.

Visual Studio 的組建工具只會安裝您建立 C 和 c + + 程式所需的命令列編譯器、工具和程式庫。The Build Tools for Visual Studio installs only the command-line compilers, tools, and libraries you need to build C and C++ programs. 這適用于組建實驗室或教室練習,並以相對快速的方式進行安裝。It's perfect for build labs or classroom exercises and installs relatively quickly. 若只要安裝命令列工具,請在 [ Visual Studio 下載] 頁面上尋找 Visual Studio 的組建工具。To install only the command-line tools, look for Build Tools for Visual Studio on the Visual Studio Downloads page.

在您可以在命令列上建立 C 或 c + + 程式之前,請先確認已安裝工具,而且您可以從命令列存取它們。Before you can build a C or C++ program on the command line, verify that the tools are installed, and you can access them from the command line. Visual C++ 具有命令列環境的複雜需求,以尋找所使用的工具、標頭和程式庫。Visual C++ has complex requirements for the command-line environment to find the tools, headers, and libraries it uses. 您不能在一般的命令提示字元視窗中使用 Visual C++, 而不需要做一些準備工作。You can't use Visual C++ in a plain command prompt window without doing some preparation. 幸好,Visual C++ 會安裝快捷方式,以啟動已設定命令列組建環境的開發人員命令提示字元。Fortunately, Visual C++ installs shortcuts for you to launch a developer command prompt that has the environment set up for command line builds. 可惜的是,開發人員命令提示字元快捷方式及其所在位置的名稱,在幾乎每個 Visual C++ 版本和不同的 Windows 版本中都不同。Unfortunately, the names of the developer command prompt shortcuts and where they're located are different in almost every version of Visual C++ and on different versions of Windows. 您的第一個逐步解說工作是尋找要使用的正確一個。Your first walkthrough task is finding the right one to use.

注意

開發人員命令提示字元快捷方式會自動設定適用于編譯器和工具,以及任何必要標頭和程式庫的正確路徑。A developer command prompt shortcut automatically sets the correct paths for the compiler and tools, and for any required headers and libraries. 如果您使用一般的 [命令提示字元] 視窗,就必須自行設定這些環境值。You must set these environment values yourself if you use a regular Command Prompt window. 建議您使用開發人員命令提示字元快捷方式,而不是自行建立。We recommend you use a developer command prompt shortcut instead of building your own.

開啟開發人員命令提示字元Open a developer command prompt

如果您已在 Windows 10 上安裝 Visual Studio 2017 或更新版本,請開啟 [開始] 功能表,然後選擇 [所有應用程式]。If you have installed Visual Studio 2017 or later on Windows 10, open the Start menu and choose All apps. 向下並開啟 [ Visual Studio ] 資料夾(而不是 Visual Studio 應用程式)。Scroll down and open the Visual Studio folder (not the Visual Studio application). 選擇 [針對 VS 開發人員命令提示字元],開啟 [命令提示字元] 視窗。Choose Developer Command Prompt for VS to open the command prompt window.

如果您已在 Windows 10 上安裝 Microsoft Visual C++ Build Tools 2015,請開啟 [開始] 功能表,然後選擇 [所有應用程式]。If you have installed Microsoft Visual C++ Build Tools 2015 on Windows 10, open the Start menu and choose All apps. 向下滾動並開啟 [ Visual C++ 組建工具] 資料夾。Scroll down and open the Visual C++ Build Tools folder. 選擇 [ Visual C++ 2015 x86 Native Tools 命令提示字元] 以開啟 [命令提示字元] 視窗。Choose Visual C++ 2015 x86 Native Tools Command Prompt to open the command prompt window.

您也可以使用 Windows 搜尋函式來搜尋「開發人員命令提示字元」,並選擇哪一個符合您安裝的 Visual Studio 版本。You can also use the Windows search function to search for "developer command prompt" and choose one that matches your installed version of Visual Studio. 使用快捷方式來開啟 [命令提示字元] 視窗。Use the shortcut to open the command prompt window.

接下來,確認 Visual C++ 的開發人員命令提示字元已正確設定。Next, verify that the Visual C++ developer command prompt is set up correctly. 在 [命令提示字元] 視窗中,輸入 cl 並確認輸出看起來像這樣:In the command prompt window, enter cl and verify that the output looks something like this:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise>cl

Microsoft (R) C/C++ Optimizing Compiler Version 19.10.25017 for x86

Copyright (C) Microsoft Corporation. All rights reserved.

usage: cl [ option... ] filename... [ /link linkoption... ]

目前目錄或版本號碼可能有差異。There may be differences in the current directory or version numbers. 這些值取決於 Visual C++ 的版本以及任何已安裝的更新。These values depend on the version of Visual C++ and any updates installed. 如果上述輸出類似于您所看到的內容,則您可以在命令列中建立 C 或 c + + 程式。If the above output is similar to what you see, then you're ready to build C or C++ programs at the command line.

注意

如果您收到錯誤,例如「' cl ' 無法辨識為內部或外部命令、可執行檔程式或批次檔」、「錯誤 C1034 或錯誤 LNK1104,當您執行命令時, cl 可能是因為您未使用開發人員命令提示字元,或您安裝 Visual C++ 時發生問題。If you get an error such as "'cl' is not recognized as an internal or external command, operable program or batch file," error C1034, or error LNK1104 when you run the cl command, then either you are not using a developer command prompt, or something is wrong with your installation of Visual C++. 您必須先修正此問題,才能繼續。You must fix this issue before you can continue.

如果您找不到開發人員命令提示字元快捷方式,或是在輸入時收到錯誤訊息 cl ,則您的 Visual C++ 安裝可能會發生問題。If you can't find the developer command prompt shortcut, or if you get an error message when you enter cl, then your Visual C++ installation may have a problem. 請嘗試重新安裝 Visual Studio 中的 Visual C++ 元件,或重新安裝 Microsoft Visual C++ 組建工具。Try reinstalling the Visual C++ component in Visual Studio, or reinstall the Microsoft Visual C++ Build Tools. 請不要進入下一節,直到 cl 命令運作為止。Don't go on to the next section until the cl command works. 如需安裝和疑難排解 Visual C++ 的詳細資訊,請參閱安裝 Visual Studio。For more information about installing and troubleshooting Visual C++, see Install Visual Studio.

注意

視電腦上的 Windows 版本和系統安全性設定而定,您可能必須以滑鼠右鍵按一下開啟 [開發人員命令提示字元] 快捷方式的快捷方式功能表,然後選擇 [以系統管理員身分執行],成功建立並執行您依照本逐步解說所建立的程式。Depending on the version of Windows on the computer and the system security configuration, you might have to right-click to open the shortcut menu for the developer command prompt shortcut and then choose Run as administrator to successfully build and run the program that you create by following this walkthrough.

建立 Visual C++ 原始程式檔,並在命令列上進行編譯Create a Visual C++ source file and compile it on the command line

在 [開發人員命令提示字元] 視窗中,輸入 md c:\hello 以建立目錄,然後輸入 cd c:\hello 以變更至該目錄。In the developer command prompt window, enter md c:\hello to create a directory, and then enter cd c:\hello to change to that directory. 此目錄是您在其中建立來源檔案和已編譯器的位置。This directory is where your source file and the compiled program are created in.

notepad hello.cpp在 [命令提示字元] 視窗中輸入。Enter notepad hello.cpp in the command prompt window.

當 [記事本] 提示您建立檔案時,選擇 [是] 。Choose Yes when Notepad prompts you to create a file. 此步驟會開啟空白的 [記事本] 視窗,您可以在名為 hello .cpp 的檔案中輸入您的程式碼。This step opens a blank Notepad window, ready for you to enter your code in a file named hello.cpp.

在 [記事本] 中,輸入下列程式程式碼:In Notepad, enter the following lines of code:

#include

using namespace std;

int main()

{

cout << "Hello, world, from Visual C++!" << endl;

}

這段程式碼是一個簡單的程式,會在螢幕上書寫一行文字,然後結束。This code is a simple program that will write one line of text on the screen and then exit. 若要將錯誤數降至最低,請複製此程式碼,再將其貼到記事本中。To minimize errors, copy this code and paste it into Notepad.

儲存您的工作!Save your work! 在 [記事本] 的 [檔案] **** 功能表中,選擇 [儲存] ****。In Notepad, on the File menu, choose Save.

恭喜,您已建立可供編譯的 c + + 原始程式檔(即 hello .cpp)。Congratulations, you've created a C++ source file, hello.cpp, that is ready to compile.

切換回 [開發人員命令提示字元] 視窗。Switch back to the developer command prompt window. 在命令提示字元中輸入, dir 以列出 c:\hello 目錄的內容。Enter dir at the command prompt to list the contents of the c:\hello directory. 您應該會在目錄清單中看到來源檔案 hello .cpp,如下所示:You should see the source file hello.cpp in the directory listing, which looks something like:

c:\hello>dir

Volume in drive C has no label.

Volume Serial Number is CC62-6545

Directory of c:\hello

05/24/2016 05:36 PM

05/24/2016 05:36 PM

05/24/2016 05:37 PM 115 hello.cpp

1 File(s) 115 bytes

2 Dir(s) 571,343,446,016 bytes free

您的電腦上的日期和其他詳細資料會有所不同。The dates and other details will differ on your computer. 如果您沒有看到您的原始程式碼檔案,請確定您已變更為您所建立的c: \ hello目錄。If you don't see your source code file, hello.cpp, make sure you've changed to the c:\hello directory you created. 在 [記事本] 中,請確定您已將原始程式檔儲存在此目錄中。In Notepad, make sure that you saved your source file in this directory. 此外,也請確定您已儲存具有副檔名的原始程式碼 .cpp ,而不是 .txt 延伸模組。Also make sure that you saved the source code with a .cpp file name extension, not a .txt extension.

在開發人員命令提示字元中,輸入 cl /EHsc hello.cpp 以編譯您的程式。At the developer command prompt, enter cl /EHsc hello.cpp to compile your program.

cl.exe 編譯器會產生內含編譯後之程式碼的 .obj 檔案,然後執行連結器,以建立名為 basic.exe 的可執行程式。The cl.exe compiler generates an .obj file that contains the compiled code, and then runs the linker to create an executable program named hello.exe. 此名稱會出現在編譯器所顯示輸出資訊行中。This name appears in the lines of output information that the compiler displays. 編譯器的輸出看起來應該像這樣:The output of the compiler should look something like:

c:\hello>cl /EHsc hello.cpp

Microsoft (R) C/C++ Optimizing Compiler Version 19.10.25017 for x86

Copyright (C) Microsoft Corporation. All rights reserved.

hello.cpp

Microsoft (R) Incremental Linker Version 14.10.25017.0

Copyright (C) Microsoft Corporation. All rights reserved.

/out:hello.exe

hello.obj

注意

如果您收到錯誤,例如「' cl ' 無法辨識為內部或外部命令、可運作的程式或批次檔、錯誤 C1034 或錯誤 LNK1104,您的開發人員命令提示字元並未正確設定。If you get an error such as "'cl' is not recognized as an internal or external command, operable program or batch file," error C1034, or error LNK1104, your developer command prompt is not set up correctly. 如需如何修正此問題的相關資訊,請回到開啟開發人員命令提示字元一節。For information on how to fix this issue, go back to the Open a developer command prompt section.

注意

如果您收到不同的編譯器或連結器錯誤或警告,請檢查您的原始程式碼以更正任何錯誤,然後儲存並再次執行編譯器。If you get a different compiler or linker error or warning, review your source code to correct any errors, then save it and run the compiler again. 如需特定錯誤的詳細資訊,請使用 [搜尋] 方塊尋找錯誤號碼。For information about specific errors, use the search box to look for the error number.

若要執行 hello.exe 程式,請在命令提示字元下,輸入 hello。To run the hello.exe program, at the command prompt, enter hello.

程式會顯示下列文字並結束:The program displays this text and exits:

Hello, world, from Visual C++!

恭喜,您已使用命令列工具來編譯並執行 c + + 程式。Congratulations, you've compiled and run a C++ program by using the command-line tools.

後續步驟Next steps

這個 "Hello,World" 範例就像 c + + 程式可以取得的一樣簡單。This "Hello, World" example is about as simple as a C++ program can get. 真實世界的程式通常會有標頭檔、更多的來源檔案,以及程式庫。Real world programs usually have header files, more source files, and link to libraries.

您可以使用本逐步解說中的步驟來建立您自己的 c + + 程式碼,而不是輸入所顯示的範例程式碼。You can use the steps in this walkthrough to build your own C++ code instead of typing the sample code shown. 這些步驟也可讓您建立多個在其他地方找到的 c + + 程式碼範例程式。These steps also let you build many C++ code sample programs that you find elsewhere. 您可以將原始程式碼放在任何可寫入的目錄中,並建立應用程式。You can put your source code and build your apps in any writeable directory. 根據預設,Visual Studio IDE 會在 [來源 \ 存放庫] 子資料夾中,于您的使用者資料夾中建立專案。By default, the Visual Studio IDE creates projects in your user folder, in a source\repos subfolder. 較舊的版本可能會將專案放在*檔 \ Visual Studio \ *專案] 資料夾中。Older versions may put projects in a *Documents\Visual Studio \*Projects* folder.

若要編譯具有其他原始程式碼檔案的程式,請在命令列上輸入這些檔案,如下所示:To compile a program that has additional source code files, enter them all on the command line, like:

cl /EHsc file1.cpp file2.cpp file3.cpp

/EHsc命令列選項會指示編譯器啟用標準 c + + 例外狀況處理行為。The /EHsc command-line option instructs the compiler to enable standard C++ exception handling behavior. 如果沒有它,擲回的例外狀況可能會導致 undestroyed 物件和資源流失。Without it, thrown exceptions can result in undestroyed objects and resource leaks.

當您提供其他來源檔案時,編譯器會使用第一個輸入檔來建立程式名稱。When you supply additional source files, the compiler uses the first input file to create the program name. 在此情況下,它會輸出名為 file1.exe 的程式。In this case, it outputs a program called file1.exe. 若要將名稱變更為 program1.exe,請新增/out連結器選項:To change the name to program1.exe, add an /out linker option:

cl /EHsc file1.cpp file2.cpp file3.cpp /link /out:program1.exe

若要自動攔截更多程式設計錯誤,建議您使用/W3或/W4警告層級選項進行編譯:And to catch more programming mistakes automatically, we recommend you compile by using either the /W3 or /W4 warning level option:

cl /W4 /EHsc file1.cpp file2.cpp file3.cpp /link /out:program1.exe

編譯器(cl.exe)有更多選項。The compiler, cl.exe, has many more options. 您可以將它們套用至組建、優化、程式碼和分析。You can apply them to build, optimize, debug, and analyze your code. 如需快速清單,請 cl /? 在開發人員命令提示字元中輸入。For a quick list, enter cl /? at the developer command prompt. 您也可以分別編譯和連結,並在更複雜的組建案例中套用連結器選項。You can also compile and link separately and apply linker options in more complex build scenarios. 如需編譯器和連結器選項和使用方式的詳細資訊,請參閱C/c + + 建立參考。For more information on compiler and linker options and usage, see C/C++ Building Reference.

您可以使用 NMAKE 和 makefile、MSBuild 和專案檔,或 CMake,在命令列上設定並建立更複雜的專案。You can use NMAKE and makefiles, MSBuild and project files, or CMake, to configure and build more complex projects on the command line. For more information on using these tools, see NMAKE Reference, MSBuild, and CMake projects in Visual Studio.

C 和 c + + 語言類似,但並不相同。The C and C++ languages are similar, but not the same. MSVC 編譯器會使用一個簡單的規則,來決定在編譯器代碼時所要使用的語言。The MSVC compiler uses a simple rule to determine which language to use when it compiles your code. 根據預設,MSVC 編譯器會處理以 C 原始程式碼結尾的檔案 .c ,以及以 .cpp c + + 原始程式碼結尾的檔案。By default, the MSVC compiler treats files that end in .c as C source code, and files that end in .cpp as C++ source code. 若要強制編譯器將所有檔案視為與副檔名無關的 c + +,請使用/tp編譯器選項。To force the compiler to treat all files as C++ independent of file name extension, use the /TP compiler option.

MSVC 編譯器包含符合 ISO C99 標準的 C 執行時間程式庫(CRT),但有次要例外狀況。The MSVC compiler includes a C Runtime Library (CRT) that conforms to the ISO C99 standard, with minor exceptions. 可移植程式碼通常會如預期般編譯並執行。Portable code generally compiles and runs as expected. 某些過時的程式庫函式和數個 POSIX 函式名稱,已由 MSVC 編譯器取代。Certain obsolete library functions, and several POSIX function names, are deprecated by the MSVC compiler. 支援函數,但慣用的名稱已變更。The functions are supported, but the preferred names have changed.

另請參閱See also

cppan 命令_逐步解說:在命令列上編譯原生 C++ 程式相关推荐

  1. 昊鼎王五:Windows运行中的所有命令_Windows快捷命令_运行中的所有命令

    昊鼎王五:Windows运行中的所有命令_Windows快捷命令_"运行"中的所有命令 winver 检查Windows版本 wmimgmt.msc 打开Windows管理体系结构 ...

  2. dig命令使用详解|使用dig命令挖掘域名解析信息

    dig命令使用详解|使用dig命令挖掘域名解析信息 Dig命令主要用来从 DNS 域名服务器查询主机地址信息 1. 查询域名的A记录 dig qq.com #查询qq.com的A记录 root@xg: ...

  3. 华为关闭telnet命令_华为s5720配置telnet命令详解

    华为s5720配置telnet命令详解 以本地PC登录远程的Telnet_Server为例,Telent登录配置如下: 1. 使能服务器功能 system-view [HUAWEI] sysname ...

  4. centos 添加路由命令_详解CentOS 6.4 添加永久静态路由所有方法汇总

    CentOS添加永久静态路由,具体如下: 在使用双网卡,同时使用2个网关的时候就需要加一条静态路由了.当然还有很多时候会需要加路由. 操作系统版本centos-6.4 64bit 一:使用route ...

  5. mysql执行计划命令_【MySQL】EXPLAIN命令详解--解释执行计划

    具体参考: 原文如下: 在工作中,我们用于捕捉性能问题最常用的就是打开慢查询,定位执行效率差的SQL,那么当我们定位到一个SQL以后还不算完事,我们还需要知道该SQL的执行计划,比如是全表扫描,还是索 ...

  6. excel查找命令_快速查找Excel功能区命令

    excel查找命令 If you have been using the Ribbon in Excel 2007 or Excel 2010 for a while, you can probabl ...

  7. linux+gunzip解压命令,Linux gunzip解压文件命令详解

    gunzip(gnu unzip) 功能说明:解压文件. 语法:gunzip [-acfhlLnNqrtvV][-s ][文件-] 或 gunzip [-acfhlLnNqrtvV][-s ][目录] ...

  8. MySQL数据库常用命令_常用SQL语句及命令_MySQL常用语句

    文章目录 一.常用的 SQL 语句 (一)查看当前登录用户 (二)查看当前数据库 (三)如何查看全部用户? (四)创建数据库 (五)选择要操作的数据库 (六)创建表 (七)查看表的结构 (八)往表中插 ...

  9. python的几个实用命令_你不知道的几个Python命令,比Excel好用多了

    Hello大家好,欢迎来到[统计GO],本公众号定期分享一些好用的数据分析工具,帮助大家花更少的时间学更多的知识. 如果本期内容(使用Python实现Excel的三个功能)对您有所帮助,记得及时关注. ...

最新文章

  1. mysql 酒店管理设计_酒店管理系统的设计与实现(Myeclipse,MySQL)
  2. leetcode1085. 最小元素各数位之和 py不止是字符串的神!
  3. Java框架数据库连接池比较(c3p0,dbcp和proxool)
  4. python 报ImportError: Install xlrd = 1.0.0 for Excel support错误出现及其解决帮法
  5. Rust 算法排位记-选择排序图示与代码实现
  6. python3生成器_Python3.7之生成器
  7. 孪生网络Dataset
  8. Refused to load the image
  9. Triumph X与K-Hyundai美术馆将于今年4月推出现代美术NFT
  10. 【Java从0到架构师】日志处理 - SLF4J、Logback、Log4j 2.x
  11. 图像的连通域标记算法及工具介绍
  12. 三相逆变器双pi控制器参数如何调节_电机(四)——永磁同步电机PI参数调节...
  13. OpenCV蒙版的使用实例(1)
  14. GitChat 是一个怎样的产品?
  15. 独家专访腾讯云CTO王慧星:云技术变革上下二十年
  16. Android自定义按钮样式
  17. 图像几何校正-- -- -- 投影变换
  18. 模拟集成电路设计学习笔记(一)IC617工艺库安装
  19. “远程办公”用户达3.46亿,一年每人可节省1.4万
  20. 普及什么是BGP服务器。BGP机房是什么以及高防BGP

热门文章

  1. java商品展示页面代码_java学习(十四)实现商品的展示、curd以及分页展示
  2. C++ Primer 5th笔记(chap 16 模板和泛型编程)模板类型别名
  3. C++ Primer 5th笔记(chap 14 重载运算和类型转换)函数匹配与重载运算符
  4. 近世代数--子环--怎么判断是不是子环?
  5. 设计模式--观察者(Observer)模式
  6. pipe读写前设置O_NOATIME
  7. C/C++ 类库开发库参考【资料整理】
  8. 使用递归的方式实现简易的二分法
  9. Ubuntu安装apt出现报错如何操作(简单记录)
  10. 【网络安全】如何搭建MySQL恶意服务器读取文件?