使用 Vim 编译和运行 C++ 程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21474662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compiling and Running a C++ Program with Vim
提问by hhoward
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!
我知道这听起来很愚蠢,但我已经在 Vim 编辑器上编写了 C++ 代码,而且我一生都无法弄清楚如何编译和运行它。我知道这非常简单,但我已经醒了太久了谢谢您的帮助!
采纳答案by Paul Evans
Simply type into vim
:
只需输入vim
:
:wq
then, on your command line, invoke the compilerwhich isn't vim
but g++
or clang++
or something like:
然后,在命令行上,调用编译器是不是vim
,但g++
还是clang++
或者类似的东西:
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!
编辑:所有投票者请阅读该人本人 Bram Moolenaar 的 vim 用户手册的第 21 节,标题为:“走开,然后回来”。:help user-manual
会带你到那里。他基本上说在vim中编译或保存并退出,编译然后回来都很酷。根据本节的标题,我认为 Bram 更喜欢后者。他无疑已经精心制作了 vim 来以最出色的方式处理后者。当我“返回”到任何文件时:光标位于同一行和同一列,我可以像以前一样撤消和重做,我所有的历史记录、搜索、注册以及一切都与我离开时完全一样。我基本上看不出和我走的时候有什么不同!
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.
重新编辑:再次给所有失望的选民 - 窥视者出于多种不同的原因以多种不同的方式使用 vim。我一直在 shell 级别做很多事情,以至于持续留在 vim 中是非常愚蠢的。很明显,你们失望的选民在完全不同的情况下工作,在这种情况下,你很高兴留在编辑器中。请停止认为所有窥视者的工作方式都与您相同。
回答by Luc Hermitte
:make
is indeed the way to go as Jonsaid.
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).
在安装了 gnumake 的类 Linux(它也适用于 cygwin,但不适用于 Windows 上的 mingw)系统上,如果您的项目中没有 Makefile,并且您的项目仅由一个文件组成,只需键入:make
. 就足够了(您可以使用$CXXFLAGS
,$CFLAGS
并$LDFLAGS
调整编译选项)。然后要运行该程序,请键入:!./%<
(IIRC)。
If your project is made of several files, then you'll need a Makefile to take advantage of :make
.
如果您的项目由多个文件组成,那么您将需要一个 Makefile 来利用: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: BuildToolsWrapperintegrates 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_vimrcplugin.
如果您使用 CMake 管理您的项目,并且如果您在源代码树之外的目录(或多个 -> 调试、发布、...)中编译您的项目,那么集成将需要一个插件。AFAIK,我是唯一提出这样一个插件的人:BuildToolsWrapper集成了 CMake 的管理(构建目录的选择,在调试或发布之间进行选择的可能性,或任何构建目录)。它必须与local_vimrc插件之一结合使用。
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).
在所有情况下,直接从 Vim 内部(或外部)调用编译器,:!g++ -o %< %
或者使用我们 15 年前在 vi 上所做的任何事情。Vim 有一个很棒的功能:它可以集成(是的,就像在 IDE 中一样)编译器。见:h quickfix
。直接从编辑器在错误之间导航比用我们的眼睛提取一个错误行、在编辑器中输入行号、返回到 shell 看看究竟是什么被拒绝要容易得多,......在 C 中可能就足够了,但是在 C++ 中,当我们“尝试调用不存在的重载”时,我们不能以这种方式工作(在编辑器和 shell 之间来回切换)。
回答by Jon
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.
这取决于您如何构建软件。如果您使用的是 make,则可以:make
在 vim 提示符中输入。键入:h make
以查看可让您循环查看代码中的错误的选项和支持命令。