Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

vim

windows-7

I'm trying to set up Vim to user VS (express) C compiler 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:

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

and the loads myfile.c into VS's IDE! Even if cl.exe needs its environment:

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.

like image 743
gvkv Avatar asked Apr 06 '11 03:04

gvkv


People also ask

How do I compile a GCC file in Vim?

:se makeprg=gcc\ -o\ %<\ % and then use the vim make command to compile the current file and get a quickfixlist of errors in vim: :make EDIT: If you also want to run the compiled executable from within vim you can do ('!' executes]

How do I compile a java file in Vim?

For working with Java, I run :compiler javac (you're choosing the Vim file in that folder called javac.vim, not the command javac - although it will ultimately be used), and after that I can type :make % to compile my current Java file. % is essentially "the current file" to Vim.

How to compile C program with Visual Studio Code?

Compiling C program with Visual Studio Code using Windows Compilation Compilation is a process of converting the source code into machine or object code. It is done with the help of the compiler. The compiler checks the source code for the errors, and if no error arises, then it generates the machine code.

Is there a way to make a makefile in Vim?

There are several possibilities. Where the easiest Makefile would look like. Others can explain this a lot better. Show activity on this post. The canonical way to do this in Vim is to use the compiler configuration setting. Your vim installation almost certainly comes with a compiler plugin for gcc.


1 Answers

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. In particular

  1. cl.exe requires the correct environment as well as the correct path; i.e., adding C:\my\path\to\cl isn't sufficient. Otherwise you will get errors reporting on missing DLLs. The solution is to run vcvars32.bat (or any other batch file which sets up a similar environment) and cl as a single command.

  2. cmd requires any paths with spaces to be double quoted but you can't escape them with \ because :! ... 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):

: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).

like image 180
gvkv Avatar answered Sep 20 '22 02:09

gvkv