C++ 直接从vim编译
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/540721/
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
compile directly from vim
提问by Sungwon Jeong
I'd like to compile cpp file w/o turning off vi.
I know the :!g++ file.cppbut I prefer :makeso I added this line in .vimrc file
我想在不关闭 vi 的情况下编译 cpp 文件。
我知道:!g++ file.cpp但我更喜欢:make所以我在 .vimrc 文件中添加了这一行
au FileType C set makeprg=gcc\ %
au FileType Cpp set makeprg=g++\ %
au 文件类型 C 设置 makeprg=gcc\ %
au 文件类型 Cpp 设置 makeprg=g++\ %
but I keep getting
"make: ***** No targets specified and no makefile found. Stop.** "message.
can anyone tell me what is wrong with my setting?
I use to compile successfully with the option above.
但我不断收到
“make: ***** 未指定目标且未找到 makefile。停止。**”消息。
谁能告诉我我的设置有什么问题?
我使用上面的选项成功编译。
采纳答案by Sungwon Jeong
I should change C,Cppinto c,cpp, then it works fine.
thank you all, especially Rob Wells, your answer helped me a lot. thank you.
我应该将C,Cpp更改为c,cpp,然后它就可以正常工作了。
谢谢大家,尤其是Rob Wells,你的回答对我帮助很大。谢谢你。
回答by Rob Wells
You need the substitution there, try something like:
您需要在那里进行替换,请尝试以下操作:
set makeprg=gmake\ %:r.o
Oh, this assumes that you've got:
哦,这假设你有:
- a (M|m)akefile in the directory, or
- default SUFFIX rules are available for your environment (which it looks like there aren't)
- 目录中的 (M|m)akefile,或
- 默认 SUFFIX 规则可用于您的环境(看起来没有)
Check for the default by entering:
通过输入检查默认值:
make -n <my_file>.o
and see if that gives you something sensible.
看看这是否给你一些明智的东西。
If there is a makefile in another location you can add the -f option to point at the makefile, for example:
如果在其他位置有 makefile,您可以添加 -f 选项以指向 makefile,例如:
set makeprg=gmake\ -f\ ../some_other_dir/makefile\ %:r.o
BTW For learning about make, and especially gmake, I'd suggest having a look at the excellent book "Managing Projects with GNU Make" (sanitised Amazon link).
顺便说一句,要了解 make,尤其是 gmake,我建议看一看优秀的书“Managing Projects with GNU Make”(经过消毒的 Amazon 链接)。
HTH.
哈。
cheers
干杯
回答by Jonas
I think it's much easier if you write a Makefile and put it where vi can find it. I'm not sure if you actually use vi (I've only used Vim), but when there is a Makefile compiling should be as easy as writing :make(no set makeprg needed).
我认为如果你写一个 Makefile 并将它放在 vi 可以找到它的地方会容易得多。我不确定你是否真的使用过 vi(我只使用过 Vim),但是当有 Makefile 时,编译应该像编写:make一样简单(不需要 set makeprg)。
回答by padfoot27
It can be easily achieved by the use of key maps.
它可以通过使用键映射轻松实现。
First open up your vimrc file and these lines to the file,
首先打开你的 vimrc 文件和文件中的这些行,
autocmd filetype cpp nnoremap <F4> :!g++ % -ggdb -o %:r <CR>
autocmd filetype cpp nnoremap<F5> :!g++ % -ggdb -o %:r && ./%:r <CR>
The first line maps the key F4 to compiling the file. The second line maps the key F5 to compile and run.
第一行将键 F4 映射到编译文件。第二行将键 F5 映射到compile 和 run。
If you use gdb frequently then this may also come handy.
如果您经常使用 gdb,那么这也可能会派上用场。
autocmd filetype cpp nnoremap<F10> :!g++ % -ggdb -o %:r && gdb -tui %:r <CR>
This line maps the key F10 to compile and start gdb
这一行映射了 F10 键来编译和启动 gdb
Hope this helps.
希望这可以帮助。
回答by user480776
I recommend a vim plugin called SingleCompile instead of what you have done: http://www.vim.org/scripts/script.php?script_id=3115
我推荐一个名为 SingleCompile 的 vim 插件,而不是你所做的:http: //www.vim.org/scripts/script.php?script_id=3115
回答by Whaledawg
First of all, just make the bloody make file. Every tool out there is expecting to work with make and if your compilations are that simple it takes about 30 seconds to write a make file that compiles all c and cpp files into an executable.
首先,只需制作该血腥的make文件。那里的每个工具都期望与 make 一起使用,如果您的编译如此简单,则编写一个将所有 c 和 cpp 文件编译为可执行文件的 make 文件大约需要 30 秒。
Second, if you refuse to use a make file then try
其次,如果您拒绝使用 make 文件,请尝试
:help system
That should give you enough info to come up with your own command similar to this
这应该为您提供足够的信息来提出与此类似的您自己的命令
:com Mymake call system("g++ ".expand("%"))