简体   繁体   English

如何在Windows 7上使用Visual Studio的编译器使用Vim的'make'命令编译C程序?

[英]How do I compile C programs using Vim's 'make' command with Visual Studio's compiler on Windows 7?

I'm trying to set up Vim to user VS (express) C compiler cl.exe . 我正在尝试将Vim设置为用户VS(快速)C编译器cl.exe Adding 添加

set makrprg='c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\cl.exe'

(I've tried escaping with \\\\ , \\\\\\ , \\\\\\\\ just to be sure) to my _vimrc file and invoking :make % returns the following: (我已经尝试使用\\\\\\\\\\\\\\\\\\以确保转义到我的_vimrc文件并调用:make %返回以下内容:

:! myfile.c >C:\Users\gvkv\AppData\Local\Temp\VIe7BF5.tmp 2>&1

and the loads myfile.c into VS's IDE! 并将myfile.c加载到VS的IDE中! Even if cl.exe needs its environment: 即使cl.exe需要它的环境:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat

this is still strange and I have no idea how to proceed. 这仍然很奇怪,我不知道如何继续。

Judge Maygarden's solution is correct up to proper escaping and solved the Vim problem I had but there were a couple of other issues that I had to resolve to make things work. Maygarden法官的解决方案是正确的,可以解决我遇到的Vim问题,但还有一些其他问题需要我解决才能使事情发生。 In particular 尤其是

  1. cl.exe requires the correct environment as well as the correct path; cl.exe需要正确的环境以及正确的路径; ie, adding C:\\my\\path\\to\\cl isn't sufficient. 即,将C:\\my\\path\\to\\cl是不够的。 Otherwise you will get errors reporting on missing DLL s. 否则,您将收到有关丢失DLL的错误报告。 The solution is to run vcvars32.bat (or any other batch file which sets up a similar environment) and cl as a single command. 解决方案是运行vcvars32.bat (或任何其他设置类似环境的批处理文件)和cl作为单个命令。

  2. cmd requires any paths with spaces to be double quoted but you can't escape them with \\ because :! ... cmd要求任何带空格的路径都是双引号但你不能用\\来转义它们因为:! ... :! ... treats \\ literally; :! ...对待\\字面; instead, you must double quote the double quote, ""... . 相反,你必须双引双引号, ""...

So, for completeness I thought I'd post my actual solution. 所以,为了完整性,我想我会发布我的实际解决方案。 In Vim (or _vimrc ): 在Vim(或_vimrc )中:

:set makeprg=\"\"Program\ Files\ (x86)\\Microsoft\ Visual\ Studio\ 10.0\\VC\\bin\\vcvars32.bat\"\&\&cl\"

and then you can simply call 然后你可以简单地打电话

:make %

and you're done. 你完成了

This method is easily generalizable for any compiler. 对于任何编译器,此方法都很容易推广。 Also, as devemouse's answer suggests, creating a makefile for nmake is probably the best way to go about doing things for any non-trivially sized project (I wanted a Vim-only solution that suits my current work). 此外,正如devemouse的回答所暗示的那样,为nmake创建一个makefile可能是为任何非平凡大小的项目做事的最佳方式(我想要一个适合我当前工作的仅限Vim的解决方案)。

I created a makefile where I had such target: 我创建了一个makefile ,我有这样的目标:

VCPROJ = /path/to/MyProject.vcproj

all:
    "c:\Program Files\Microsoft Visual Studio 9.0\VC\vcpackages\vcbuild.exe" /nocolor /r $(VCPROJ) Debug

.PHONY: all

I had make in vim's path so did not have to modify makeprg . 我只好make Vim的路径,并没有修改makeprg

This way VS compiled normally whole project and vim parsed the errors. 这种方式VS正常编译整个项目并且vim解析了错误。

I believe the problem with your makeprg statement is mostly due to the spaces in the path. 我相信你的makeprg语句的问题主要是由于路径中的空格。 Cmd.exe requires double quotes around the spaces. Cmd.exe需要空格周围的双引号。 First, use escaped double quotes around the path (\\"). Next escape all back slashes (\\). Finally, escape all spaces (\\ ). 首先,在路径(\\“)周围使用转义双引号。接下来转义所有反斜杠(\\)。最后,转义所有空格(\\)。

set makrprg=\"c:\\Program\ Files\ (x86)\\Microsoft\ Visual\ Studio\ 10.0\\VC\\bin\\cl.exe\"

Alternatively, you could setup your PATH appropriately and just set makeprg to cl.exe directly. 或者,您可以适当地设置PATH并直接将makeprg设置为cl.exe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么使用Dev-C ++编译器而不是Visual Studio的编译器进行编译? - Why does this compile with the Dev-C++ compiler and not Visual Studio's one? 如何告诉Visual Studio / Microsoft的C编译器在第一个语句之后允许变量声明? - How can I tell Visual Studio/Microsoft's C compiler to allow variable declarations after the first statement? 如何在没有Visual Studio的情况下下载和安装Microsoft的Visual Studio C / C ++编译器 - How to download and install Microsoft's Visual Studio C/C++ compiler without Visual Studio 如何使用 Visual Studio 2012 为 Windows XP 进行编译? - How do I compile for Windows XP with Visual Studio 2012? 用Visual Studio 2005编译C程序? - Compile C programs with Visual Studio 2005? 我如何在C程序之间共享变量的值 - How do i share a variable's value between C programs vim:make如果成功则编译并运行C代码 - vim :make to compile and run C code if it's a success 如何使用Visual Studio 2010 Professional调试现有C程序? - How do I debug existing C programs with Visual Studio 2010 Professional? Vim 的 `:compiler gcc` 与 `make: ***` 有问题 - Vim's `:compiler gcc` has an issue with `make: ***` Visual Studio C编译器是否具有与GCC -M相同的功能? - Does the Visual Studio C compiler have an equivalent to GCC's -M?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM