使用Visual C ++编译(命令行)
示例
对于从GCC或Clang到VisualStudio的程序员,或者通常更熟悉命令行的程序员,可以从命令行以及IDE使用VisualC++编译器。
如果要在VisualStudio中从命令行编译代码,则首先需要设置命令行环境。可以通过打开VisualStudioCommandPrompt/DeveloperCommandPrompt/x86NativeToolsCommandPrompt/x64NativeToolsCommandPrompt或类似文件(由您的VisualStudio版本提供)或在命令提示符处导航到VC编译器安装目录的子目录(通常为\ProgramFiles(x86)\MicrosoftVisualStudiox\VC,其中x版本号为(10.0对于2010年或14.02015年),并使用VCVARSALL此处指定的命令行参数运行批处理文件。
请注意,与GCC不同,VisualStudio不会link.exe通过编译器(cl.exe)提供链接器()的前端,而是将链接器作为单独的程序提供,编译器在退出时会对其进行调用。 cl.exe并且link.exe可以与不同的文件和选项一起单独使用,cl也可以告诉他们将文件和选项传递给link两个任务是否同时完成。指定给的任何链接选项cl都将转换为的选项link,而未经处理的任何文件cl将直接传递给link。因为这主要是使用VisualStudio命令行进行编译的简单指南,所以目前link将不介绍的参数。如果您需要列表,请参见此处。
请注意,tocl的参数区分大小写,而tolink的参数则不区分大小写。
[请注意,以下某些示例%cd%在指定绝对路径名时使用Windowsshell的“当前目录”变量。对于不熟悉此变量的任何人,它将扩展到当前工作目录。在命令行中,这将是你在当您运行的目录cl,并在命令默认提示指定(如果您的命令提示符C:\src>,例如,然后%cd%是C:\src\)。]
假设main.cpp在当前文件夹中命名了一个源文件,则用于编译和链接未优化的可执行文件(用于初始开发和调试)的命令为(使用以下两种方法之一):
cl main.cpp // Generates object file "main.obj". // Performs linking with "main.obj". // Generates executable "main.exe". cl /Od main.cpp //同上。 // "/Od" 是个 "Optimisation: disabled" option, and 是个 default when no /O is specified.
假设在同一目录中有其他源文件“niam.cpp”,请使用以下命令:
clmain.cppniam.cpp // Generates object files "main.obj" and "niam.obj". // Performs linking with "main.obj" and "niam.obj". // Generates executable "main.exe".
您也可以使用通配符,就像人们期望的那样:
clmain.cppsrc\*.cpp // Generates object file "main.obj", plus one object file for each ".cpp" file in folder // "%cd%\src". // Performs linking with "main.obj", and every additional object file generated. //所有目标文件将位于当前文件夹中。 // Generates executable "main.exe".
要重命名或重定位可执行文件,请使用以下方法之一:
cl /o name main.cpp // Generates executable named "name.exe". cl /o folder\ main.cpp // Generates executable named "main.exe", in folder "%cd%\folder". cl /o folder\name main.cpp // Generates executable named "name.exe", in folder "%cd%\folder". cl /Fename main.cpp // Same as "/o name". cl /Fefolder\ main.cpp // Same as "/o folder\". cl /Fefolder\name main.cpp // Same as "/o folder\name".
都将/o和/Fe传递其参数(称为o-param)到linkas/OUT:o-param,并根据需要将适当的扩展名(通常为.exe或.dll)附加到“name”上o-param。就我所知,虽然两者/o和/Fe功能相同,但后者是VisualStudio的首选。 /o标记为已弃用,并且似乎主要是为更熟悉GCC或Clang的程序员提供的。
请注意,虽然/o指定文件夹和/或名称之间的空格是可选的,但指定文件夹和/或名称之间不能有空格/Fe。
同样,要生成优化的可执行文件(用于生产),请使用:
cl /O1 main.cpp //针对可执行文件大小进行优化。产生小程序,但可能会降低速度 //执行。 cl /O2 main.cpp //优化执行速度。产生快速程序,但可能会花费更大 //文件大小。 cl /GLmain.cppother.cpp //生成用于整个程序优化的特殊目标文件,从而使CL可以 //优化过程中要考虑到每个模块(翻译单元)。 // Passes the option "/LTCG" (Link-Time Code Generation) to LINK, telling it to call CL during //链接阶段以执行其他优化。如果此时不执行链接 // time, the generated object files should be linked with "/LTCG". //可以与其他CL优化选项一起使用。
最后,要生成特定于平台的优化可执行文件(以在具有指定体系结构的计算机上的生产中使用),请VCVARSALL为目标平台选择适当的命令提示符或参数。 link应该从目标文件中检测所需的平台;如果不是,请使用该/MACHINE选项明确指定目标平台。
//如果针对x64进行编译,而LINK不会自动检测目标平台: clmain.cpp/link /machine:X64
上面的任何一个都会产生一个由/o或指定的名称的可执行文件/Fe,或者如果没有提供,则其名称与为编译器指定的第一个源文件或目标文件相同。
cla.cppb.cpp c.cpp // Generates "a.exe". cld.obja.cpp q.cpp // Generates "d.exe". cly.libn.cpp o.obj // Generates "n.exe". cl /o yozp.objpz.cpp // Generates "yo.exe".
要编译file(s)不链接的对象,请使用:
cl /c main.cpp // Generates object file "main.obj".
这告诉cl退出而不调用link,并生成一个目标文件,该文件以后可以与其他文件链接以生成二进制文件。
clmain.objniam.cpp // Generates object file "niam.obj". // Performs linking with "main.obj" and "niam.obj". // Generates executable "main.exe". linkmain.objniam.obj // Performs linking with "main.obj" and "niam.obj". // Generates executable "main.exe".
还有其他有价值的命令行参数,这对用户了解非常有用:
cl /EHsc main.cpp // "/EHsc" specifies that only standard C++ ("synchronous") exceptions will be caught, // and `extern "C"` functions will not throw exceptions. //建议在编写可移植的,独立于平台的代码时使用。 cl /clr main.cpp // "/clr" specifies that the code should be compiled to use the common language runtime, //.NETFramework的虚拟机。 // Enables the use of Microsoft's C++/CLI language in addition to standard ("native") C++, //并创建一个需要.NET才能运行的可执行文件。 cl /Za main.cpp // "/Za" specifies that Microsoft extensions should be disabled, and code should be //严格按照ISOC++规范进行编译。 //建议您这样做以确保可移植性。 cl /Zi main.cpp // "/Zi" generates a program database (PDB) file for use when debugging a program, without // affecting optimisation specifications, and passes the option "/DEBUG" to LINK. cl /LD dll.cpp // "/LD" tells CL to configure LINK to generate a DLL instead of an executable. //链接时,除了LIB和EXP文件外,LINK还将输出一个DLL。 //要在其他程序中使用DLL,请在编译它们时将其关联的LIB传递给CL或LINK。 //程式。 clmain.cpp/link /LINKER_OPTION // "/link" passes everything following it directly to LINK, without parsing it in any way. // Replace "/LINKER_OPTION" with any desired LINK option(s).
对于任何更熟悉*nix系统和/或,GCC/锵cl,link和其他VisualStudio命令行工具可以接受以连字符指定的参数(例如-c),而不是一个斜杠(如/c)。此外,Windows将斜杠或反斜杠识别为有效的路径分隔符,因此也可以使用*nix样式的路径。这样,只需很少的更改即可轻松地将简单的编译器命令行从g++或转换clang++为cl,反之亦然。
g++ -o app src/main.cpp cl -o app src/main.cpp
当然,当移植使用更复杂g++或更多clang++选项的命令行时,您需要在适用的编译器文档中和/或在资源站点上查找等效的命令,但这使在花费最少时间学习新编译器的情况下更轻松地开始工作。。
如果您的代码需要特定的语言功能,则需要特定版本的MSVC。从VisualC++2015Update3开始,可以通过该/std标志选择要编译的标准版本。可能的值为/std:c++14和/std:c++latest(/std:c++17将很快出现)。
注意:在此编译器的旧版本中,可以使用特定的功能标志,但是该功能标志主要用于预览新功能。