Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling and Running a C++ Program with Vim [duplicate]

Tags:

c++

vim

I know this sounds really dumb, but I've written a C++ code on Vim editor and for the life of me cannot figure out how to compile and run it. I know this is extremely simple but I've been awake for far too long Thanks for the help!

like image 595
hhoward Avatar asked Jan 31 '14 07:01

hhoward


2 Answers

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

like image 144
Luc Hermitte Avatar answered Sep 27 '22 17:09

Luc Hermitte


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.

like image 37
Jon Avatar answered Sep 27 '22 18:09

Jon