views:

2515

answers:

5

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.

thank you.

+4  A: 

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

Jonas
thank you for your answer. but I use the editor for very smallprojeccts like project euler problems.so everytime I write a code, making Makefile is quite irritating. that's why I am trying to use the :make(direct compile).
Jace Jung
+2  A: 

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

Rob Wells
I find out what my problem was. it was about case of C,Cpp.I should change it into c,cpp. but your answer was very helpful.Thank you.
Jace Jung
@Luc, ?? Or is $*.o a new vim makeprg syntax?
Rob Wells
Luc Hermitte
A: 

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("%"))
Whaledawg
Why do you think it is better than `:!g++ %`?
Marius Gedminas
+2  A: 

I finally find out!
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.

Jace Jung
Also change `set` to `setlocal` in order not to override default setting for other filetypes.
ZyX
A: 

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