从 cmd 运行 cl.exe

kee*_*ner 9 c++ visual-studio

我已经安装Visual Studio Community 2017了 C++。我想从 cmd 使用它的编译器。我可以使用它,Developer Command Prompt for VS 2017但无法通过普通 cmd 使用它。我曾尝试运行vsvarsall.exeright click-> run as administrator。但什么也没有发生。好像我必须手动设置环境变量。每当我尝试运行命令时

cl hello.c

它说 hello.c(1): fatal error C1034: stdio.h: no include path set

cbu*_*art 8

Visual Studio 包含一个为您准备环境的批处理文件(实际上,开发人员命令提示符在后台调用它)。

我从未尝试过社区版,但对于 VS 2017 Professional,它位于"%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build\vcvars32.bat". 当然,如果您更改了安装路径,它可能会有所不同。

所以,你所要做的就是调用它:

call "%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build\vcvars32.bat"
Run Code Online (Sandbox Code Playgroud)

应该出现如下内容

**********************************************************************
** Visual Studio 2017 Developer Command Prompt v15.7.3
** Copyright (c) 2017 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x86'
Run Code Online (Sandbox Code Playgroud)

After that you can invoke cl, nmake, msbuild as within cmd.

You can also invoke vcvarsall.bat x86 instead (the vcvars32.bat is just a shortcut for that).


You can avoid typing it each time by creating a batch that automatically invokes it and then open a command prompt

call "%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build\vcvars32.bat"
cmd
Run Code Online (Sandbox Code Playgroud)

And then run that batch instead of cmd.

Another option is to add the "%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build\" to the path so you only have to type vcvars32.bat when you need the developer tools.