Compiling and Running a C++ Program with Vim

:make is indeed the way to go as Jon said.

On Linux-like (it also applies to cygwin, but not to mingw on windows) systems where gnumake is installed, if you don't have a Makefile in your project, and if your project is made of only one file, just type :make. It will be enough (you can play with $CXXFLAGS, $CFLAGS and $LDFLAGS to tune the compilation options). Then to run the program, type :!./%< (IIRC).

If your project is made of several files, then you'll need a Makefile to take advantage of :make.

If you manage your project with CMake, and if you compile your project in a directory (or several -> debug, release, ...) outside the sources tree, then the integration will require a plugin. AFAIK, I'm the only one to propose such a plugin: BuildToolsWrapper integrates the management of CMake (choice of the build directory, possibility to chose between the debug, or release, or whatever build directory). It has to be coupled with one of the local_vimrc plugin.

In all cases, calling directly the compiler from within (or outside) Vim with :!g++ -o %< % or whatever is what we used to do 15 years ago on vi. Vim has a wonderful feature: it can integrate (yes, like in IDE) the compiler. See :h quickfix. Navigating between errors directly from the editor is much easier than extracting one error line with our eyes, typing back the line number into the editor, going back to the shell to see what exactly was rejected, ... It may be enough in C, but In C++ when we are "trying to call an overload that doesn't exist", we can't work this way (switching back and forth between the editor and the shell).


This depends on how you build your software. If you're using make, you can enter :make in the vim prompt. Type :h make to see the options and supporting command that let you cycle through the errors in your code.


Simply type into vim:

:wq

then, on your command line, invoke the compiler which isn't vim but g++ or clang++ or something like:

g++ -o myCode myCode.cpp

EDIT: All you down voters please read section 21 of the vim user's manual by the man himself Bram Moolenaar entitled: "Go away and come back". :help user-manual will get you there. Where he basically says it's cool to either compile in vim OR save and exit, compile, and then come back. By the title of the section I presume Bram prefers the later. He most certainly has crafted vim to handle the later in a most excellent manner. When I "come back" to any file: the cursor is at the same row and column, I can undo and redo as before, and all my history, searches, registers, and everything is exactly as I left it. I basically can't see any difference from when I went away!

RE-EDIT: to all you down voters again - peeps use vim in so many different ways for so many different reasons. I do so much at the shell level all the time that it would be painfully idiotic to remain continuously in vim. You down voters obviously work in completely different situations where you are good to stay inside the editor. Please stop thinking all peeps work the same way as you do.

Tags:

C++

Vim