如何直接从Windows中的vim编辑器编译cpp文件? [英] How to compile a cpp file directly from vim editor in Windows?

查看:91
本文介绍了如何直接从Windows中的vim编辑器编译cpp文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在Windows操作系统中安装了vim编辑器.我只知道常规过程,即在编辑器中创建源文件并从命令行运行它.但,我不太了解如何直接从vim编辑器中编译CPP程序.当我运行命令时

:! g ++ hello.cpp -o您好

从vim命令行中,我收到以下消息

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

按任意键关闭此窗口...

解决方案

我假设您的Vim版本是Windows版本,而不是cygwin版本.

首先,您需要安装编译器,并确保它在您的PATH中.

另外,请阅读有关quickfix窗口的文档,因为这是集成的 vim的编译方式. :! make :! g ++ ... 并非可行之路.

MSVC

我不认为这是您拥有的编译器,因为我希望您在那种情况下使用过Visual Studio.无论如何,IIRC,都有一个可以使用:compiler msdev 加载的 msdev 编译器插件,那么您应该可以运行:make .

如果看到错误,请立即完成我的回答.

g ++通过cygwin

有一个很大的优点:gnumake的配置正确:在控制台中,您可以运行 make foo ,并且如果您具有 foo.cpp foo.c 并且在当前目录中没有 Makefile ,这将编译monofile项目.在所有情况下, Makefile 都可以;并且它是多个源文件所必需的.

最大的问题:路径名的表达方式不同.他们需要翻译.我在Build-Tools-Wrapper插件中提供了一种方法.只需执行:BTW添加cygwin .

然后从vim中再次输入:make%< .它将转换为:make foo (假设您正在编辑 foo.cpp ),它将明智地转换为 make foo shell,然后转换为 $ CXX $ CPPFLAGS $ CXXFLAGS $ LDFLAGS foo.cpp -o foo $ LDLIBS (或类似的东西).

注意:这意味着可以使用以下选项调整选项::let $ CXXFLAGS ='-std = c ++ 17 -Wall -Wextra'

顺便说一句,如果您有我的build-tools-wrapper插件,则可以直接执行:Make 而不是:make%< 或仅执行< F5> ,即IIRC.

g ++通过mingw

好消息:无需转换路径名

坏消息,gnumake的配置不正确.这意味着在控制台中, make foo 将不起作用.因此,这在Vim中是行不通的.

这一次,您将需要一个 Makefile ,或者您需要调整'makeprg'设置.例如,例如:let& makeprg ='g ++ -Wall -Wextra -std = c ++ 17 -o%<%',然后只需键入:make .

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

解决方案

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.

这篇关于如何直接从Windows中的vim编辑器编译cpp文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆