compile directly from vim

怎甘沉沦 提交于 2019-12-03 04:28:51

问题


I'd like to compile cpp file w/o turning off vi.
I know the :!g++ file.cpp but I prefer :make so I added this line in .vimrc file

au FileType C set makeprg=gcc\ %
au FileType Cpp set 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.


回答1:


You need the substitution there, try something like:

set makeprg=gmake\ %:r.o

Oh, this assumes that you've got:

  1. a (M|m)akefile in the directory, or
  2. default SUFFIX rules are available for your environment (which it looks like there aren't)

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:

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

HTH.

cheers




回答2:


I should change C,Cpp into c,cpp, then it works fine.

thank you all, especially Rob Wells, your answer helped me a lot. thank you.




回答3:


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




回答4:


It can be easily achieved by the use of key maps.

First open up your vimrc file and these lines to the file,

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.

If you use gdb frequently then this may also come handy.

autocmd filetype cpp nnoremap<F10> :!g++ % -ggdb -o %:r && gdb -tui %:r <CR>

This line maps the key F10 to compile and start gdb

Hope this helps.




回答5:


I recommend a vim plugin called SingleCompile instead of what you have done: http://www.vim.org/scripts/script.php?script_id=3115




回答6:


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.

Second, if you refuse to use a make file then try

:help system

That should give you enough info to come up with your own command similar to this

:com Mymake call system("g++ ".expand("%"))


来源:https://stackoverflow.com/questions/540721/compile-directly-from-vim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!