How to compile a cpp file directly from vim editor in Windows?

时光总嘲笑我的痴心妄想 提交于 2020-12-27 05:27:44

问题


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


回答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.



来源:https://stackoverflow.com/questions/61006639/how-to-compile-a-cpp-file-directly-from-vim-editor-in-windows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!