尝试编译和调试 C++ 来自维姆

发布于 2024-07-15 01:10:07 字数 283 浏览 7 评论 0原文

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

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

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

谢谢。

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

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?

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

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

淡笑忘祈一世凡恋 2024-07-22 01:10:07

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

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

如需学习,请参阅手册

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.

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

For learning make see the manual

知你几分 2024-07-22 01:10:07

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

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

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

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

强制编辑: 我不使用 Windows 或 Cygwin。 您可能需要采纳 epochwolf 的建议那个部门。

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

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

To learn more about GNU Make and Makefiles:

Also, see this question: compile directly from vim

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

习惯成性 2024-07-22 01:10:07

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

:!gcc file.c -o file

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

:!gdb

希望它能帮助你。

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:

:!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

I hope it will help you.

心碎的声音 2024-07-22 01:10:07

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

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

通常,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

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

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

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:

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

淡写薰衣草的香 2024-07-22 01:10:07

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

GNU make 教程< /a> (这个使用 gcc 和 make,所以它应该是你的拿手好戏)

另一个

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


是的,无需 makefile 即可编译。 如果您的程序很简单(例如,只有一个文件),您可以通过调用编译器并将程序名称包含在同一行中来进行编译(不记得它与 gcc 的配合方式)。 当然,为了让事情变得更容易,可以将其映射到 vim 中的键,这样您就不必跳转到命令提示符并返回。

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

好吧,就是这样。只是我的 0.2 欧元:)

EDIT1: A few bookmarks you might find useful:

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

Another one

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


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.

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.

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

坦然微笑 2024-07-22 01:10:07

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

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

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

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

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 :make from vim, and that's enough.

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 &makeprg in consequence, and finally call :make from 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).

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)

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.

彩扇题诗 2024-07-22 01:10:07

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

http://pastebin.com/qc5Zp2P7

它假设您正在使用 g++ 编译器。 如果没有,只需将名称更改为您正在使用的编译器。 将此文件保存到安装vim/ftplugin/cpp.vim 的任何位置。

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

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.

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文