compile directly from vim

后端 未结 6 1176
独厮守ぢ
独厮守ぢ 2021-01-31 05:43

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

相关标签:
6条回答
  • 2021-01-31 06:18

    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

    0 讨论(0)
  • 2021-01-31 06:25

    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.

    0 讨论(0)
  • 2021-01-31 06:30

    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("%"))
    
    0 讨论(0)
  • 2021-01-31 06:33

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

    0 讨论(0)
  • 2021-01-31 06:34

    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.

    0 讨论(0)
  • 2021-01-31 06:38

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

    0 讨论(0)
提交回复
热议问题