在cmd命令行下使用cl编译运行C/C++源文件

前提条件: 安装了Visual Studio。 我安装的是Visual Studio 2010 Express。

操作步骤[1]:

1)编辑源代码文件。例如D:\foo\main.c

2)启动cmd。 start -> app -> Microsoft Visual Studio 2010 Express-> Visual Studio Command Prompt (2010)

3)改变工作目录,进入源代码文件所在文件目录,例如d:\foo。

4)在cmd中输入命令 cl main.c,回车。

运行结果:

Setting environment for using Microsoft Visual Studio 2010 x86 tools.C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC>d:D:\&;cd fooD:\foo>cl main.cMicrosoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86Copyright (C) Microsoft Corporation. All rights reserved.main.cMicrosoft (R) Incremental Linker Version 10.00.40219.01Copyright (C) Microsoft Corporation. All rights reserved./out:main.exemain.objD:\foo>dir Volume in drive D has no label. Volume Serial Number is A8F3-9697 Directory of D:\foo\foo08/31/2015 03:55 PM <DIR>.08/31/2015 03:55 PM <DIR>..08/16/2015 09:13 AM446 main.c08/31/2015 03:55 PM45,568 main.exe08/31/2015 03:55 PM1,452 main.obj5 File(s)118,150 bytes2 Dir(s) 111,024,001,024 bytes freeD:\foo>main.exeHello, world.D:\foo>

编译过程中可能出现的错误:

1) 链接错误:fatal error LNK1104: cannot open file ‘kernel32.lib’

错误处理:向系统环境变量LIB添加kernel32.lib的保存路径。

链接器link找不过kernel32.lib的原因可能是环境变量LIB中未包含保存kernel32.lib的路径。 在我的机器上,,kernel32.lib的保存路径为:C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Lib。

在我的电脑->属性->高级->环境变量->系统变量 中找到 系统变量LIB,如果找不到就新建系统变量LIB。将C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Lib; 添加到系统变量LIB中。

cl编译器命令选项

C/C++ COMPILER OPTIONS-OPTIMIZATION-/O1 minimize space/O2 maximize speed/Ob<n> inline expansion (default n=0) /Od disable optimizations (default)/Og enable global optimization/Oi[-] enable intrinsic functions/Os favor code space/Ot favor code speed/Ox maximum optimizations/Oy[-] enable frame pointer omission-CODE GENERATION-/GF enable read-only string pooling/Gm[-] enable minimal rebuild/Gy[-] separate functions for linker /GS[-] enable security checks/GR[-] enable C++ RTTI/GX[-] enable C++ EH (same as /EHsc)/EHs enable C++ EH (no SEH exceptions) /EHa enable C++ EH (w/ SEH exceptions)/EHc extern "C" defaults to nothrow/fp:<except[-]|fast|precise|strict> choose floating-point model:except[-] – consider floating-point exceptions when generating codefast – "fast" floating-point model; results are less predictableprecise – "precise" floating-point model; results are predictablestrict – "strict" floating-point model (implies /fp:except)/Qfast_transcendentals generate inline FP intrinsics even with /fp:except/GL[-] enable link-time code generation /GA optimize for Windows Application/Ge force stack checking for all funcs /Gs[num] control stack checking calls/Gh enable _penter function call/GH enable _pexit function call/GT generate fiber-safe TLS accesses /RTC1 Enable fast checks (/RTCsu)/RTCc Convert to smaller type checks /RTCs Stack Frame runtime checking/RTCu Uninitialized local usage checks /clr[:option] compile for common language runtime, where option is:pure – produce IL-only output file (no native executable code)safe – produce IL-only verifiable output fileoldSyntax – accept the Managed Extensions syntax from Visual C++ 2002/2003initialAppDomain – enable initial AppDomain behavior of Visual C++ 2002noAssembly – do not produce an assembly/Gd __cdecl calling convention/Gr __fastcall calling convention/Gz __stdcall calling convention/GZ Enable stack checks (/RTCs)/QIfist[-] use FIST instead of ftol() /hotpatch ensure function padding for hotpatchable images/arch:<SSE|SSE2|AVX> minimum CPU architecture requirements, one of:SSE – enable use of instructions available with SSE enabled CPUsSSE2 – enable use of instructions available with SSE2 enabled CPUsAVX – enable use of Intel(R) Advanced Vector Extensions instructions/Qimprecise_fwaits generate FWAITs only on "try" boundaries, not inside "try"/Qsafe_fp_loads generate safe FP loads-OUTPUT FILES-/Fa[file] name assembly listing file /FA[scu] configure assembly listing/Fd[file] name .PDB file/Fe<file> name executable file/Fm[file] name map file/Fo<file> name object file/Fp<file> name precompiled header file /Fr[file] name source browser file/FR[file] name extended .SBR file/Fi[file] name preprocessed file/doc[file] process XML documentation comments and optionally name the .xdc file-PREPROCESSOR-/AI<dir> add to assembly search path /FU<file> forced using assembly/module /C don't strip comments/D<name>{=|#}<text> define macro/E preprocess to stdout/EP preprocess to stdout, no #line/P preprocess to file/Fx merge injected code to file/FI<file> name forced include file/U<name> remove predefined macro/u remove all predefined macros/I<dir> add to include search path/X ignore "standard places"-LANGUAGE-/Zi enable debugging information/Z7 enable old-style debug info/Zp[n] pack structs on n-byte boundary /Za disable extensions/Ze enable extensions (default)/Zl omit default library name in .OBJ/Zg generate function prototypes/Zs syntax check only/vd{0|1|2} disable/enable vtordisp/vm<x> type of pointers to members/Zc:arg1[,arg2] C++ language conformance, where arguments can be:forScope[-] – enforce Standard C++ for scoping ruleswchar_t[-] – wchar_t is the native type, not a typedefauto[-] – enforce the new Standard C++ meaning for autotrigraphs[-] – enable trigraphs (off by default)/ZI enable Edit and Continue debug info /openmp enable OpenMP 2.0 language extensions-MISCELLANEOUS-@<file> options response file/?, /help print this help message/bigobj generate extended object format /c compile only, no link/errorReport:option Report internal compiler errors to Microsoftnone – do not send reportprompt – prompt to immediately send reportqueue – at next admin logon, prompt to send report (default)send – send report automatically/FC use full pathnames in diagnostics /H<num> max external name length/J default char type is unsigned/MP[n] use up to 'n' processes for compilation/nologo suppress copyright message/showIncludes show include file names/Tc<source file> compile file as .c/Tp<source file> compile file as .cpp/TC compile all files as .c/TP compile all files as .cpp/V<string> set version string/w disable all warnings/wd<n> disable warning n/we<n> treat warning n as an error/wo<n> issue warning n once/w<l><n> set warning level 1-4 for n/W<n> set warning level (default n=1) /Wall enable all warnings/WL enable one line diagnostics/WX treat warnings as errors/Yc[file] create .PCH file/Yd put debug info in every .OBJ/Yl[sym] inject .PCH ref for debug lib /Yu[file] use .PCH file/Y- disable all PCH options/Zm<n> max memory alloc (% of default)/Wp64 enable 64 bit porting warnings-LINKING-/LD Create .DLL/LDd Create .DLL debug library/LN Create a .netmodule/F<num> set stack size/link [linker options and libraries] /MD link with MSVCRT.LIB/MT link with LIBCMT.LIB/MDd link with MSVCRTD.LIB debug lib/MTd link with LIBCMTD.LIB debug lib-优化- /O1 最小化空间/Op[-] 改善浮点数一致性 /O2 最大化速度/Os 优选代码空间 /Oa 假设没有别名/Ot 优选代码速度 /Ob<n> 内联展开(默认 n=0)/Ow 假设交叉函数别名 /Od 禁用优化(默认值)/Ox 最大化选项。(/Ogityb2 /Gs) /Og 启用全局优化/Oy[-] 启用框架指针省略 /Oi 启用内部函数 -代码生成- /G3 为 80386 进行优化/Gh 启用 _penter 函数调用 /G4 为 80486 进行优化/GH 启用 _pexit 函数调用 /G5 为 Pentium 进行优化/GR[-] 启用 C++ RTTI /G6 对 PPro、P-II、P-III 进行优化/GX[-] 启用 C++ EH (与 /EHsc 相同) /G7 对 Pentium 4 或 Athlon 进行优化/EHs 启用 C++ EH (没有 SEH 异常) /GB 为混合模型进行优化(默认)/EHa 启用 C++ EH(w/ SEH 异常) /Gd __cdecl 调用约定/EHc extern "C" 默认为 nothrow /Gr __fastcall 调用约定/GT 生成纤维安全 TLS 访问 /Gz __stdcall 调用约定/Gm[-] 启用最小重新生成 /GA 为 Windows 应用程序进行优化/GL[-] 启用链接时代码生成 /Gf 启用字符串池/QIfdiv[-] 启用 Pentium FDIV 修复 /GF 启用只读字符串池/QI0f[-] 启用 Pentium 0x0f 修复 /Gy 分隔链接器函数/QIfist[-] 使用 FIST 而不是 ftol() /GZ 启用堆栈检查(/RTCs)/RTC1 启用快速检查(/RTCsu) /Ge 对所有函数强制堆栈检查/RTCc 转换为较小的类型检查 /Gs[num] 控制堆栈检查调用/RTCs 堆栈帧运行时检查 /GS 启用安全检查/RTCu 未初始化的本地用法检查 /clr[:noAssembly] 为公共语言运行库编译noAssembly – 不产生程序集 /arch:<SSE|SSE2> CPU 结构的最低要求,以下内容之一:SSE – 启用支持 SSE 的 CPU 可用的指令SSE2 – 启用支持 SSE2 的 CPU 可用的指令 -输出文件- /Fa[file] 命名程序集列表文件/Fo<file> 命名对象文件 /FA[sc] 配置程序集列表/Fp<file> 命名预编译头文件 /Fd[file] 命名 .PDB 文件/Fr[file] 命名源浏览器文件 /Fe<file> 命名可执行文件/FR[file] 命名扩展 .SBR 文件 /Fm[file] 命名映射文件 -预处理器- /AI<dir> 添加到程序集搜索路径/Fx 将插入的代码合并到文件 /FU<file> 强制使用程序集/模块/FI<file> 命名强制包含文件 /C 不抽出注释/U<name> 移除预定义宏 /D<name>{=|#}<text> 定义宏/u 移除所有预定义宏 /E 预处理到 stdout/I<dir> 添加到包含搜索路径 /EP 预处理到 stdout,没有 #line/X 忽略“标准位置” /P 预处理到文件 -语言- /Zi 启用调试信息/Ze 启用扩展(默认) /ZI 启用“编辑并继续”调试信息/Zl 省略 .OBJ 中的默认库名 /Z7 启用旧式调试信息/Zg 生成函数原型 /Zd 仅有行号调试信息/Zs 只进行语法检查 /Zp[n] 在 n 字节边界上包装结构/vd{0|1} 禁用/启用 vtordisp /Za 禁用扩展(暗指 /Op)/vm<x> 指向成员的指针类型 /Zc:arg1[,arg2] C++ 语言一致性,这里的参数可以是:forScope – 对范围规则强制使用标准 C++wchar_t – wchar_t 是本机类型,不是 typedef – 杂项 – @<file> 选项响应文件/wo<n> 发出一次警告 n /?, /help 打印此帮助消息/w<l><n> 为 n 设置警告等级 1-4 /c 只编译,不链接/W<n> 设置警告等级(默认 n=1) /H<num> 最大外部名称长度/Wall 启用所有警告 /J 默认 char 类型是 unsigned/Wp64 启用 64 位端口定位警告 /nologo 取消显示版权消息/WX 将警告视为错误 /showIncludes 显示包含文件名/WL 启用单行诊断 /Tc<source file> 将文件编译为 .c/Yc[file] 创建 .PCH 文件 /Tp<source file> 将文件编译为 .cpp/Yd 将调试信息放在每个 .OBJ 中 /TC 将所有文件编译为 .c/Yl[sym] 为调试库插入 .PCH 引用 /TP 将所有文件编译为 .cpp/Yu[file] 使用 .PCH 文件 /V<string> 设置版本字符串/YX[file] 自动 .PCH /w 禁用所有警告/Y- 禁用所有 PCH 选项 /wd<n> 禁用警告 n/Zm<n> 最大内存分配(默认为 %) /we<n> 将警告 n 视为错误 -链接- /MD 与 MSVCRT.LIB 链接/MDd 与 MSVCRTD.LIB 调试库链接 /ML 与 LIBC.LIB 链接/MLd 与 LIBCD.LIB 调试库链接 /MT 与 LIBCMT.LIB 链接/MTd 与 LIBCMTD.LIB 调试库链接 /LD 创建 .DLL/F<num> 设置堆栈大小 /LDd 创建 .DLL 调试库/link [链接器选项和库]

参考文献

[1] Walkthrough: Compiling a C Program on the Command Line. Hadi Braishttps://msdn.microsoft.com/en-us/library/bb384838.aspx

版权声明:本文为博主原创文章,未经博主允许不得转载。

可就是这样,还是有人,期望过多的温暖。

在cmd命令行下使用cl编译运行C/C++源文件

相关文章:

你感兴趣的文章:

标签云: