views:

2197

answers:

7

I've been learning C++ and Allegro using Code::Blocks on Windows XP, and recently felt like learning Vim. However, I have no idea how to compile a program "manually" (i.e., without using an IDE).

For compiling from within Vim, I have gotten as far as setting the makeprg to gcc, but I understand I also need a makefile. What is that and how do I write one? Is it with the makefile that I can use libraries such as Allegro?

Also, I've gotten quite fond of the CB debugger (I'm using gdb). Is it possible to do something similar with Vim?

Thank you.

+4  A: 

You don't necessarily need a Makefile, but it's the preferred (and possibly sanest) way of compiling code on UNIX-like systems.

I don't know if GNU Make has a Windows port, but you can probably run it under Cygwin.

To learn more about GNU Make and Makefiles:

Also, see this question: compile directly from vim

Mandatory edit: I don't use Windows or Cygwin. You might want to take epochwolf's advice on that department.

Can Berk Güder
davr
Hey, I'm just saying. I wouldn't know if Cygwin is any good, I don't use Windows. =)
Can Berk Güder
+1  A: 

EDIT1: A few bookmarks you might find useful:

GNU make tutorial (this one uses gcc and make, so it should be right up your alley)

Another one

Win port of some of GNU utils Can was mentioning; I personally use these and haven't had any problems with them on Windows platform.


Yes, you can compile without the makefile. If your program is simple (for example, one file only) you can compile by calling the compiler and including the name of the program in the same line (don't remember how it goes with gcc). Of course, to make things easier this can be mapped to a key within vim, so you don't have to jump to command prompt and back.

If you are working on a bigger project, which consists of several files and such, than a makefile is useful. It will "search" through the files, determine dependencies, include them in the build, maybe put the source files in one directory and the resulting exe file in the other and such. So it is more of a linking and building system than just compiling. Although the GNU make mentioned in Can Berk Guder's answer is a popular one, there are quite a number of other tooks for "building makefiles" ("makefile" has become a type of synonym for that kind of operation) - here, you can see some other options on this link. Due to its part in history vim has good support for :make, but others can be easily used as well (there are a lot of texts on this subject on VimWikia.

Well, that's it. Just my 0,2 euros :)

ldigas
+3  A: 

Look at MinGW. I would avoid Cygwin if you only need gcc and make. You'll want both MinGW and MSYS. MSYS has a windows port of make.

If you're interested in more unix utlities for the windows command line I recommend CoreUtils.

For learning make see the manual

epochwolf
+2  A: 

I'm not in expert in makefiles and debugging but I know that Vim lets you do many things. For example if you want to compile a file with gcc, it's not very different from the usual way. In normal mode type:

:!gcc file.c -o file

In fact you can use (almost) every system command just by adding "!" in front of your command. gdb also works with Vim

:!gdb

I hope it will help you.

Taurus Olson
+1  A: 

As long as you have GNU-make installed (with cygwin or mingw under windows), you don't need to write a makefile for single-file projects. Just run :make from vim, and that's enough.

If your project is made of several files, then you will have to write a makefile (or any equivalent for scons, aap, (b)jam, ant, ...), tune your &makeprg in consequence, and finally call :make from vim. See the relevant category in vimtips. You can of course run the compiler as you would have ran any other external tool, but you would loose the possibility to jump to the line(s) of the error(s).

NB: if you are using the win32 version of vim, and gcc-cygwin, you'll need to translate the error messages obtained. I used to maintain a perl script for this purpose, it is now part of a bigger suite (still in beta stage)

Regarding your question about debugging, it can't be done from vim under windows for the moment. The only debugger that can be integrated so far is gdb, but under linux only ; see the pyclewn (.sf.net) project.

Luc Hermitte
+1  A: 

To integrate vim with devenv, you can use devenv from Visual Studio to complie the project in vim. The command as follows:

Devenv SolutionName /build SolnConfigName [/project ProjName [/projectconfig ProjConfigName]]

Typicatlly, the devenv should located in C:\Program Files\Microsoft Visual Studio 8\Common7\IDE. Set it to the path of environment to make it callable from vim. We also need to make vim recognize the error messages thrown by the commandline build tool devenv. Just put the following lines in your vimrc:

" Quickfix mode: command line devenv error format
au FileType cpp set makeprg=devenv.com\ /Build\ Debug\ *[SolutionName]*
au FileType cpp set errorformat=\ %#%f(%l)\ :\ %#%t%[A-z]%#\ %m

Then You can use :make to compile and build the program from vim.

CyberMing
A: 

I'm not sure about debugging, but I know an easy way to compile and run the current program as I wrote a small vim plugin to do so.

http://pastebin.com/qc5Zp2P7

It assumes you are using the g++ compiler. If not, just change the name to the compiler you're using. Save this file to whereveryouinstalledvim/ftplugin/cpp.vim

In order to compile and run the currently open program, just type shift-e while in non-editing mode.

ErikT