compile directly from vim

和自甴很熟 提交于 2019-12-02 18:47:32

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

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.

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

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.

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

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