Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim :make to compile and run C code if it's a success

Tags:

c

vim

g++

to compile C programs and run it, I use ":make" but I would like to run the compiled program too if the compile is a success.

I wrote

au FileType c setlocal makeprg=g++\ %\ \&\&\ ./a.out

in vimrc, that works, but I have a vim error when there's some mistakes in my code, so vim don't put the cursor on the good line. I get this error :

E40: Can't open errorfile /tmp/vEtUQQ2/0

Is there a workaround, a fix or another way to achieve this ?

like image 326
Gilles Quenot Avatar asked Nov 04 '10 23:11

Gilles Quenot


2 Answers

You could create a target in your makefile to run the program (say 'run'):

.PHONY : run
run : $(PROG) # assuming $(PROG) is the name of your program
    ./$(PROG)

...and then in vim you would do:

:make run
like image 137
Martin Broadhurst Avatar answered Sep 28 '22 07:09

Martin Broadhurst


There is a way with pure vim to do it, but it is a bit of annoying.

Using QuickFixCmdPost(Autocmd Event) to check if there is building error after ':make' ran. And if there are no errors, run the newly compiled program.

autocmd QuickfixCmdPost make call AfterMakeC()
function! AfterMakeC()
    " No any error after make
    if len(getqflist()) == 0
        !./a.out
    endif
    " :~)
endfunction

You may want to put the script under namespace in compiler plugin

like image 31
Mike Lue Avatar answered Sep 28 '22 08:09

Mike Lue