Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compile directly from vim

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.

like image 421
Sungwon Jeong Avatar asked Feb 12 '09 10:02

Sungwon Jeong


People also ask

Does vim have a compiler?

Vim typically has a number of common compiler configurations installed, as mentioned, so it will automatically choose the appropriate one. To actually use the compiler configuration you need to use the Vim :make function, as also mentioned, but the default mode of operation expects a Makefile to exist.


2 Answers

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

like image 53
Rob Wells Avatar answered Oct 15 '22 01:10

Rob Wells


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.

like image 27
Sungwon Jeong Avatar answered Oct 15 '22 00:10

Sungwon Jeong