visual-studio 从普通命令行使用 Visual Studio 的“cl”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/84404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Visual Studio's 'cl' from a normal command line
提问by Tom Hennen
Visual Studio 2003 and 2005 (and perhaps 2008 for all I know) require the command line user to run in the 'Visual Studio Command Prompt'. When starting this command prompt it sets various environment variables that the C++ compiler, cl, uses when compiling.
Visual Studio 2003 和 2005(据我所知可能是 2008 年)要求命令行用户在“Visual Studio 命令提示符”中运行。启动此命令提示符时,它会设置 C++ 编译器 cl 在编译时使用的各种环境变量。
This is not always desirable. If, for example, I want to run 'cl' from within Ant, I'd like to avoid having to run Ant from within the 'Visual Studio Command Prompt'. Running vcvars32.bat isn't an option as the environment set by vcvars32.bat would be lost by the time cl was run (if running from within Ant).
这并不总是可取的。例如,如果我想从 Ant 中运行“cl”,我想避免必须从“Visual Studio 命令提示符”中运行 Ant。运行 vcvars32.bat 不是一个选项,因为 vcvars32.bat 设置的环境会在 cl 运行时丢失(如果从 Ant 中运行)。
Is there an easy way to run cl without having to run from within the Visual Studio command prompt?
有没有一种简单的方法来运行 cl 而不必从 Visual Studio 命令提示符中运行?
回答by Ville Laurikari
The compilers can be used from command line (or makefiles) just like any other compilers. The main things you need to take care of are the INCLUDE and LIB environment variables, and PATH. If you're running from cmd.exe, you can just run this .bat to set the environment:
编译器可以像任何其他编译器一样从命令行(或生成文件)中使用。您需要注意的主要事项是 INCLUDE 和 LIB 环境变量以及 PATH。如果你从 cmd.exe 运行,你可以运行这个 .bat 来设置环境:
C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat
C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat
If you're trying to use the compilers from a makefile, Cygwin, MinGW, or something like that you need to set the environment variables manually. Assuming the compiler is installed in the default location, this should work for the Visual Studio 2008 compiler and the latest Windows SDK:
如果您尝试使用来自 makefile、Cygwin、MinGW 或类似文件的编译器,您需要手动设置环境变量。假设编译器安装在默认位置,这应该适用于 Visual Studio 2008 编译器和最新的 Windows SDK:
Add to PATH:
添加到路径:
- C:\Program Files\Microsoft SDKs\Windows\v6.1\Bin
- C:\Program Files\Microsoft Visual Studio 9.0\VC\Bin
- C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE
- C:\Program Files\Microsoft SDKs\Windows\v6.1\Bin
- C:\Program Files\Microsoft Visual Studio 9.0\VC\Bin
- C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE
Add to INCLUDE:
添加到包括:
- C:\Program Files\Microsoft SDKs\Windows\v6.1\Include
- C:\Program Files\Microsoft Visual Studio 9.0\VC\include
- C:\Program Files\Microsoft Visual Studio 9.0\VC\atlmfc\include
- C:\Program Files\Microsoft SDKs\Windows\v6.1\Include
- C:\Program Files\Microsoft Visual Studio 9.0\VC\include
- C:\Program Files\Microsoft Visual Studio 9.0\VC\atlmfc\include
Add to LIB:
添加到 LIB:
- C:\Program Files\Microsoft SDKs\Windows\v6.1\Lib
- C:\Program Files\Microsoft Visual Studio 9.0\VC\lib
- C:\Program Files\Microsoft SDKs\Windows\v6.1\Lib
- C:\Program Files\Microsoft Visual Studio 9.0\VC\lib
These are the bare minimum, but should be enough for basic things. Study the vcvarsall.bat script to see what more you may want to set.
这些是最低限度的,但对于基本的事情应该足够了。研究 vcvarsall.bat 脚本,看看您可能还想设置什么。
回答by Eclipse
Create your own batch file (say clenv.bat), and call that instead of cl:
创建您自己的批处理文件(比如 clenv.bat),并调用它而不是 cl:
@echo off
:: Load compilation environment
call "C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat"
:: Invoke compiler with any options passed to this batch file
"C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\cl.exe" %*
clenv.bat can now be invoked just like cl.exe, except that it will first load the needed environment variables first.
clenv.bat 现在可以像 cl.exe 一样被调用,除了它会首先加载所需的环境变量。
回答by ljs
You can simply run the batch file which sets the variables yourself. In VS08 it's located at:-
您可以简单地运行自己设置变量的批处理文件。在 VS08 中,它位于:-
C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat
回答by Michael Burr
What the vcvars32 or vsvars32 batch files do is not rocket science. They simply set the PATH, INCLUDE, LIB, and possibly the LIBPATH environment variables to sensible defaults for the particular compiler version.
vcvars32 或 vsvars32 批处理文件的作用不是火箭科学。他们只是将 PATH、INCLUDE、LIB 以及可能的 LIBPATH 环境变量设置为特定编译器版本的合理默认值。
All you have to do is make sure that those things are set properly for your Ant or makefile (either before invoking them or within them).
您所要做的就是确保为您的 Ant 或 makefile 正确设置这些内容(在调用它们之前或在它们内部)。
For INCLUDE and LIB/LIBPATH an alternative to setting those items in environment variables is to pass those settings to to command line as explicit parameters.
对于 INCLUDE 和 LIB/LIBPATH,在环境变量中设置这些项目的另一种方法是将这些设置作为显式参数传递给命令行。
回答by Michael Burr
The trick is to always use the correct vcvars batch file. IF you have just one version of VisualStudio installed, that's no big problem. If you're dealing with multiple versions like me, it becomes very easy to run a MSVC++ 14 build in a console that was set up with a MSVC++ 15 vcvars file. It might or might not work, but whatever you're getting will be different from what you'd be building from within VisualStudio.
诀窍是始终使用正确的 vcvars 批处理文件。如果您只安装了一个版本的 VisualStudio,那也没什么大问题。如果您像我一样处理多个版本,则在设置了 MSVC++ 15 vcvars 文件的控制台中运行 MSVC++ 14 构建变得非常容易。它可能有效,也可能无效,但无论您得到什么,都将与您在 VisualStudio 中构建的不同。
We have dealt with that issue in terpby deriving the proper vcvars file from the chosen compiler and always setting up the environment internally to the tool invocation. This way, you always have the right vcvars file for the compiler you're using.
我们已经处理了这个问题TERP从选择的编译器推导出正确的vcvars文件,并始终在内部设置环境的工具调用。这样,您始终拥有适合您正在使用的编译器的 vcvars 文件。
Just to reiterate: I highly recommend against trying to duplicate manually what the vcvars file does for you. You're bound to miss something or get it just right enough that it looks like it's working while actually doing something slightly different from what you wanted.
重申一下:我强烈建议不要尝试手动复制 vcvars 文件为您所做的事情。你肯定会错过一些东西,或者做得恰到好处,看起来它在工作,而实际上做的事情与你想要的略有不同。
回答by Ray Hayes
The vcvarsall.bat batch file which is run by the Visual Studio command prompt is simply trying to keep your system environment variables and paths nice and clean (and is important if you have multiple versions of Visual Studio).
由 Visual Studio 命令提示符运行的 vcvarsall.bat 批处理文件只是试图使您的系统环境变量和路径保持干净整洁(如果您有多个版本的 Visual Studio,这一点很重要)。
If you're happy limiting your setup to one version and having a long path and set of environment variables, transfer these settings (manually) to the System Environment Variables (My Computer|Properties --- or Win-Pause/Break).
如果您乐于将设置限制为一个版本并且有很长的路径和一组环境变量,请将这些设置(手动)传输到系统环境变量(我的电脑|属性 --- 或 Win-Pause/Break)。
I'd recommend against this though!
不过我建议不要这样做!
回答by Joel Purra
My version of opening the visual studio command line for Visual Studio Command Prompt in visual-studio-2010. Used internally to build a library/project and then perform some extra steps with the resulting DLL files.
我在visual-studio-2010 中为 Visual Studio 命令提示符打开 Visual Studio 命令行的版本。在内部用于构建库/项目,然后对生成的 DLL 文件执行一些额外的步骤。
Copy these lines to your Compile and execute other steps.cmdfile, or similar.
将这些行复制到您的Compile and execute other steps.cmd文件或类似文件中。
@echo off
REM Load Visual Studio's build tools
call "%ProgramFiles(x86)%\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
REM Choose what you want to do, 1 or 2 by (un)commenting
REM 1. Add your cl.exe (or msbuild.exe or other) commands here
REM msbuild.exe MyProject.csproj
REM cl.exe
REM custom-step.exe %*
REM pause
REM 2. Open a normal interactive system command shell with all variables loaded
%comspec% /k
In this version of the script, I "stay" in interactive command line mode afterwards. Comment to REM %comspec% /kto only use the script for non-interactive purposes.
在这个版本的脚本中,我之后“停留”在交互式命令行模式中。注释以REM %comspec% /k仅将脚本用于非交互目的。

