C++ 使用 cl.exe 进行命令行编译?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7865432/
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
Command line compile using cl.exe?
提问by Daniel Gratz
Am trying to use the Visual Studio Express 2010 C++ compiler without using the IDE. I found cl.exe in C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin. However am having a few difficulties. Firstly it gave me a warning pop up when i type cl saying 'Program cannot start because mspdb100.dll is missing from your computer.'
我试图在不使用 IDE 的情况下使用 Visual Studio Express 2010 C++ 编译器。我在 C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin 中找到了 cl.exe。但是我有一些困难。首先,当我输入 cl 说“程序无法启动,因为您的计算机中缺少 mspdb100.dll”时,它给了我一个警告弹出窗口。
So i add C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE to the system path and then try again, but this time:
所以我将 C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE 添加到系统路径,然后再试一次,但这一次:
fatal error C1510: Cannot load language resource clui.dll.
致命错误 C1510:无法加载语言资源 clui.dll。
Any idea how to solve this so i can compile? Also how would i set up the path so i can just type 'cl main.cpp' etc, from within a solution folder that does not contain cl.exe. At the moment i have to be inside bin folder. Thanks.
知道如何解决这个问题以便我可以编译吗?另外,我将如何设置路径,以便我可以从不包含 cl.exe 的解决方案文件夹中键入“cl main.cpp”等。目前我必须在 bin 文件夹内。谢谢。
回答by jsvk
Try starting the Visual Studio Command Prompt from
尝试从以下位置启动 Visual Studio 命令提示符
Start->
All Programs ->
Microsoft Visual Studio 2010 ->
Visual Studio Tools ->
Visual Studio Command Prompt 2010
Alternatively, you can set up the environment by running this in a command prompt:
或者,您可以通过在命令提示符下运行来设置环境:
"c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
(note: this will leave your environment set up after running.)
(注意:这将在运行后保留您的环境设置。)
(note2: change x86
as desired. options are x86
, ia64
, amd64
, x86_amd64
, x86_ia64
)
(注 2:x86
根据需要更改。选项为x86
、ia64
、amd64
、x86_amd64
、x86_ia64
)
From there you can run cl.exe
. If you want this to be automatically done and undone whenever you run cl
, create a batch file with this content:
从那里你可以运行cl.exe
. 如果您希望在运行时自动完成并撤消此操作cl
,请创建一个包含以下内容的批处理文件:
@echo off
%comspec% /c ""c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86 && cl.exe %*"
(the /c
tells the command prompt to end the session after running this command, so your environment returns to normal.)
(/c
告诉命令提示符在运行此命令后结束会话,以便您的环境恢复正常。)
From there, name it cl.bat
. Put this in a folder somewhere, and add the path to that folder to your PATH
environment variable, making sure it comes beforethe path to cl.exe
, so that this cl.bat
is executed whenever you type cl
instead of cl.exe
从那里,命名它cl.bat
。将其放在某个文件夹中,并将该文件夹的路径添加到您的PATH
环境变量中,确保它位于 to 的路径之前cl.exe
,以便cl.bat
在您键入时执行此操作cl
而不是cl.exe
I recommend you just put cl.bat
in your system32/
folder, it should come before cl.exe
's path on a default installation.
我建议你只放在cl.bat
你的system32/
文件夹中,它应该cl.exe
在默认安装的路径之前。
Alternatively, you can add it in any order and always type cl.bat
, or name it something else so there's no confusion.
或者,您可以按任何顺序添加它并始终键入cl.bat
,或将其命名为其他名称,以免混淆。
回答by Anthony Oyovwe
This is a quite simple and strait forward task. Firstly add the compiler path to system path.:C:\Program Files\Microsoft Visual Studio 10.0\VC\bin; Next, open command prompt and change directory to your source folder; Then execute the vcvars32.bat file to setup the environment for using vc++ on x86; After this, you can now type cl to compile your program
这是一项非常简单而艰巨的任务。首先将编译器路径添加到系统路径:C:\Program Files\Microsoft Visual Studio 10.0\VC\bin;接下来,打开命令提示符并将目录更改为您的源文件夹;然后执行vcvars32.bat文件,搭建x86上使用vc++的环境;在此之后,您现在可以键入 cl 来编译您的程序
回答by hege
Just use vcvarsall.bat
as jsvk suggested:
只需vcvarsall.bat
按照 jsvk 建议使用:
"c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
After that, use devenv.exe
to build your stuff if you keep yourself safe from a lot of head-aches:
在那之后,devenv.exe
如果你让自己免受很多头痛的伤害,请使用它来构建你的东西:
devenv solutionfile.sln /build [ solutionconfig ] [ /project projectnameorfile [ /projectconfig name ] ]
There are a lot of other command line switches which you can check with devenv /?
.
还有许多其他命令行开关,您可以使用devenv /?
.
回答by DaddyBean
I have multiple versions of VS installed; I create a little .BAT file for each version, placed somewhere in the path, that calls the relevant "vcvarsall.bat". e.g. "vc9.bat" calls vcvarsall.bat for VS2008, while "vc10.bat" calls vcvarsall.bat for VS2010. I can open a normal command window as usual, type "vc9", and presto, that command window is ready to compile etc using VS2008.
我安装了多个版本的 VS;我为每个版本创建了一个小 .BAT 文件,放置在路径中的某个位置,它调用相关的“vcvarsall.bat”。例如,“vc9.bat”为 VS2008 调用 vcvarsall.bat,而“vc10.bat”为 VS2010 调用 vcvarsall.bat。我可以像往常一样打开一个普通的命令窗口,输入“vc9”,然后很快,那个命令窗口已经准备好使用 VS2008 进行编译等。
回答by Yochai Timmer
That errors occurs when the linker runs out of memory.
You can use the x64 tool architecture Visual studio tools.
当链接器内存不足时会发生该错误。
您可以使用 x64 工具架构 Visual Studio 工具。
msbuild command line:
msbuild 命令行:
msbuild myproject.vcxproj /p:PreferredToolArchitecture=x64
Or, better yet, add it to the project's settings in the .vcxproj.
To specify the 64-bit version of the compiler and tools, add the following property group element to the Myproject.vcxproj project file after the Microsoft.Cpp.default.props element:
或者,更好的是,将其添加到 .vcxproj 中的项目设置中。
要指定 64 位版本的编译器和工具,请将以下属性组元素添加到 Myproject.vcxproj 项目文件的 Microsoft.Cpp.default.props 元素之后:
<PropertyGroup>
<PreferredToolArchitecture>x64</PreferredToolArchitecture>
</PropertyGroup>
These options will make Visual studio use the tools under the amd64 architecture folders (according to the target architecture):
VC\bin\amd64
VC\bin\amd64_x86
VC\bin\amd64_arm
这些选项将使 Visual Studio 使用 amd64 架构文件夹下的工具(根据目标架构):
VC\bin\amd64
VC\bin\amd64_x86
VC\bin\amd64_arm