windows 尝试从 Vim 编译和调试 C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/656176/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:10:03  来源:igfitidea点击:

Trying to compile and debug C++ from Vim

windowsvimdebuggingmakefilecompilation

提问by Caio

I've been learning C++ and Allegro using Code::Blocks on Windows XP, and recently felt like learning Vim. However, I have no idea how to compile a program "manually" (i.e., without using an IDE).

我一直在使用 Windows XP 上的 Code::Blocks 学习 C++ 和 Allegro,最近想学习 Vim。但是,我不知道如何“手动”编译程序(即,不使用 IDE)。

For compiling from within Vim, I have gotten as far as setting the makeprg to gcc, but I understand I also need a makefile. What is that and how do I write one? Is it with the makefile that I can use libraries such as Allegro?

为了在 Vim 中进行编译,我已经将 makeprg 设置为 gcc,但我知道我还需要一个 makefile。那是什么,我该如何写?我可以使用诸如 Allegro 之类的库吗?

Also, I've gotten quite fond of the CB debugger (I'm using gdb). Is it possible to do something similar with Vim?

另外,我非常喜欢 CB 调试器(我使用的是 gdb)。是否可以用 Vim 做类似的事情?

Thank you.

谢谢你。

回答by Can Berk Güder

You don't necessarily needa Makefile, but it's the preferred (and possibly sanest) way of compiling code on UNIX-like systems.

您不一定需要Makefile,但它是在类 UNIX 系统上编译代码的首选(并且可能是最明智的)方式。

I don't know if GNU Make has a Windows port, but you can probably run it under Cygwin.

我不知道 GNU Make 是否有 Windows 端口,但您可能可以在Cygwin下运行它。

To learn more about GNU Make and Makefiles:

要了解有关 GNU Make 和 Makefile 的更多信息:

Also, see this question: compile directly from vim

另外,请参阅此问题:直接从 vim 编译

Mandatory edit:I don't use Windows or Cygwin. You might want to take epochwolf's adviceon that department.

强制编辑:我不使用 Windows 或 Cygwin。您可能想在该部门听取 epochwolf 的建议

回答by epochwolf

Look at MinGW. I would avoid Cygwin if you only need gcc and make. You'll want both MinGW and MSYS. MSYS has a windows port of make.

看看MinGW。如果您只需要 gcc 和 make,我会避免使用 Cygwin。您将需要 MinGW 和 MSYS。MSYS 有一个 make 的 Windows 端口。

If you're interested in more unix utlities for the windows command line I recommend CoreUtils.

如果您对 windows 命令行的更多 unix 实用程序感兴趣,我推荐CoreUtils

For learning make see the manual

对于学习,请参阅手册

回答by Taurus Olson

I'm not in expert in makefiles and debugging but I know that Vim lets you do many things. For example if you want to compile a file with gcc, it's not very different from the usual way. In normal mode type:

我不是 makefile 和调试方面的专家,但我知道 Vim 可以让你做很多事情。比如你想用gcc编译一个文件,和通常的方式没有太大区别。在正常模式下类型:

:!gcc file.c -o file

In fact you can use (almost) every system command just by adding "!" in front of your command. gdb also works with Vim

事实上,您只需添加“!”即可(几乎)使用每个系统命令。在你的命令面前。gdb 也适用于 Vim

:!gdb

I hope it will help you.

我希望它会帮助你。

回答by AMing

To integrate vim with devenv, you can use devenv from Visual Studio to complie the project in vim. The command as follows:

要将vim 与devenv 集成,可以使用Visual Studio 中的devenv 在vim 中编译项目。命令如下:

Devenv SolutionName /build SolnConfigName [/project ProjName [/projectconfig ProjConfigName]]

Typicatlly, the devenv should located in C:\Program Files\Microsoft Visual Studio 8\Common7\IDE. Set it to the path of environment to make it callable from vim. We also need to make vim recognize the error messages thrown by the commandline build tool devenv. Just put the following lines in your vimrc:

通常,devenv 应位于 C:\Program Files\Microsoft Visual Studio 8\Common7\IDE。将其设置为环境路径以使其可从 vim 调用。我们还需要让 vim 识别命令行构建工具 devenv 抛出的错误消息。只需将以下几行放在您的 vimrc 中:

" Quickfix mode: command line devenv error format
au FileType cpp set makeprg=devenv.com\ /Build\ Debug\ *[SolutionName]*
au FileType cpp set errorformat=\ %#%f(%l)\ :\ %#%t%[A-z]%#\ %m

Then You can use :make to compile and build the program from vim.

然后你可以使用 :make 从 vim 编译和构建程序。

回答by Luc Hermitte

As long as you have GNU-make installed (with cygwin or mingw under windows), you don't need to write a makefile for single-file projects. Just run :makefrom vim, and that's enough.

只要你安装了 GNU-make(windows 下有 cygwin 或 mingw),你就不需要为单文件项目编写 makefile。只需:make从 vim运行,就足够了。

If your project is made of several files, then you will have to write a makefile (or any equivalent for scons, aap, (b)jam, ant, ...), tune your &makeprgin consequence, and finally call :makefrom vim. See the relevant category in vimtips. You can of course run the compiler as you would have ran any other external tool, but you would loose the possibility to jump to the line(s) of the error(s).

如果您的项目由多个文件组成,那么您将必须编写一个 makefile(或任何等效的 scons、aap、(b)jam、ant 等),调整您&makeprg的结果,最后:make从 vim调用。请参阅vimtips 中的相关类别。您当然可以像运行任何其他外部工具一样运行编译器,但是您将失去跳转到错误行的可能性。

NB: if you are using the win32 version of vim, and gcc-cygwin, you'll need to translate the error messages obtained. I used to maintain a perl script for this purpose, it is now part of a bigger suite(still in beta stage)

注意:如果您使用的是 vim 的 win32 版本和 gcc-cygwin,则需要翻译获得的错误消息。我曾经为此目的维护一个 perl 脚本,它现在是更大套件的一部分(仍处于测试阶段)

Regarding your question about debugging, it can't be done from vim under windows for the moment. The only debugger that can be integrated so far is gdb, but under linux only ; see the pyclewn (.sf.net) project.

关于你的调试问题,windows下的vim暂时无法完成。目前唯一可以集成的调试器是gdb,但只在linux下;请参阅 pyclewn (.sf.net) 项目。

回答by Rook

EDIT1:A few bookmarks you might find useful:

EDIT1:您可能会发现一些有用的书签:

GNU make tutorial(this one uses gcc and make, so it should be right up your alley)

GNU make 教程(这个使用 gcc 和 make,所以它应该适合你)

Another one

另一个

Win portof some of GNU utils Can was mentioning; I personally use these and haven't had any problems with them on Windows platform.

Can 提到了一些 GNU utils 的Win 端口;我个人使用这些并且在 Windows 平台上没有遇到任何问题。



Yes, you can compile without the makefile. If your program is simple (for example, one file only) you can compile by calling the compiler and including the name of the program in the same line (don't remember how it goes with gcc). Of course, to make things easier this can be mapped to a key within vim, so you don't have to jump to command prompt and back.

是的,您可以在没有 makefile 的情况下进行编译。如果您的程序很简单(例如,只有一个文件),您可以通过调用编译器并在同一行中包含程序名称来进行编译(不记得它是如何与 gcc 一起使用的)。当然,为了使事情更容易,这可以映射到 vim 中的一个键,因此您不必跳转到命令提示符并返回。

If you are working on a bigger project, which consists of several files and such, than a makefile is useful. It will "search" through the files, determine dependencies, include them in the build, maybe put the source files in one directory and the resulting exe file in the other and such. So it is more of a linking and building system than just compiling. Although the GNU make mentioned in Can Berk Guder's answer is a popular one, there are quite a number of other tooks for "building makefiles" ("makefile" has become a type of synonym for that kind of operation) - here, you can see some other options on this link. Due to its part in history vim has good support for :make, but others can be easily used as well (there are a lot of texts on this subject on VimWikia.

如果您正在处理由多个文件等组成的更大项目,那么生成文件很有用。它将“搜索”文件,确定依赖项,将它们包含在构建中,可能将源文件放在一个目录中,将生成的 exe 文件放在另一个目录中等等。因此,它更像是一个链接和构建系统,而不仅仅是编译。尽管 Can Berk Guder 的回答中提到的 GNU make 是一个流行的,但还有很多其他的“构建 makefile”(“makefile”已成为这种操作的同义词) - 在这里,你可以看到此链接上的其他一些选项。由于它在历史上的作用,vim 对 :make 有很好的支持,但其他的也很容易使用(在VimWikia上有很多关于这个主题的文本。

Well, that's it. Just my 0,2 euros :)

嗯,就是这样。只是我的 0,2 欧元 :)

回答by ErikT

I'm not sure about debugging, but I know an easy way to compile and run the current program as I wrote a small vim plugin to do so.

我不确定调试,但我知道一个简单的方法来编译和运行当前程序,因为我写了一个小的 vim 插件来这样做。

http://pastebin.com/qc5Zp2P7

http://pastebin.com/qc5Zp2P7

It assumes you are using the g++ compiler. If not, just change the name to the compiler you're using. Save this file to whereveryouinstalledvim/ftplugin/cpp.vim

它假定您使用的是 g++ 编译器。如果没有,只需将名称更改为您正在使用的编译器。把这个文件保存到你安装的地方 vim/ftplugin/cpp.vim

In order to compile and run the currently open program, just type shift-e while in non-editing mode.

为了编译和运行当前打开的程序,只需在非编辑模式下键入 shift-e。