0

I've recently installed a vim editor in my Windows operating system. I only know the conventional procedure i.e, creating the source file in the editor and running it from the command line. But, I don't quite understand how to compile a CPP program directly from the vim editor. when I run the command

:!g++ hello.cpp -o hello

from the vim command line, I get the following message

C:\WINDOWS\system32\cmd.exe /c (g++ hello.cpp -o hello)

Hit any key to close this window...

3
  • 1
    You can run programs with :!, e.g. :! g++ Commented Apr 3, 2020 at 7:07
  • Do you want to compile a single file or do you actually want to generate an executable binary? Compiling is only part of creating the latter. Commented Apr 3, 2020 at 7:47
  • Please dont post images of text, and please edit your question to use proper markdown formatting Commented Apr 3, 2020 at 14:28

1 Answer 1

1

I assume your version of Vim is windows version and not cygwin version.

First you need to install a compiler, and make sure it's in your PATH.

Also, read the documentation about quickfix window as this is the integrated vim way of compiling. :!make or :!g++ ... are not the way to go.

MSVC

I don't suppose this is the compiler you have as I expect you'd have used Visual Studio in that case. Anyway, IIRC, there is a msdev compiler plugin you could load with :compiler msdev, then you should able to run :make.

Don't hesitate to complete my answer if you see errors.

g++ through cygwin

There is a big advantage: gnumake is properly configured: in the console you could run make foo, and if you have foo.cpp or foo.c and no Makefile in the current directory, this would compile the monofile project. In all cases, a Makefile is fine; and it's required with multiple source files.

The big problem: pathnames are not expressed in the same way. They need to be translated. I provide a way to do that in my Build-Tools-Wrapper plugin. Just execute :BTW add cygwin.

Then from vim, again type :make %<. That will translate into :make foo (assuming you're editing foo.cpp), which translates into make fooshell wise, which translates into $CXX $CPPFLAGS $CXXFLAGS $LDFLAGS foo.cpp -o foo $LDLIBS (or something like that).

Note: this means the options can be tweaked with: :let $CXXFLAGS = '-std=c++17 -Wall -Wextra'

BTW, if you have my build-tools-wrapper plugin, you can execute directly :Make instead of :make %<, or just <F5>directly, IIRC.

g++ through mingw

The good news: no need to translate pathnames

The bad news, gnumake isn't correctly configured. This means that in the console make foo won't work. And consequently, this won't work from Vim.

This time, you'll either need a Makefile, or you'll need to tweak 'makeprg' setting. Like for instance :let &makeprg = 'g++ -Wall -Wextra -std=c++17 -o %< %' and then type simply :make.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Luc, Thanks for the explanation. I am currently using g++ ( MinGW) . I don't know how to create a Makefile like that. Don't take me wrong, I'm new to vim. could you please tell me how to create a make file or just drop a link that I can go to and find the answer?
The screenshot of the first link I see in google with "write Makefile" seems good and simple enough to me: youtube.com/watch?v=_r7i5X0rXJk

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.