I am using vim editor on Linux mint. I want to know if there is any way to compile c program without leaving the editor.
2 Answers
There are several possibilities.
One method is to compile using
:!gcc file.c
But a nicer strategy would be to have a Makefile and compile just using
:make
Where the easiest Makefile would look like.
program:
gcc file.c
-
2According to Is there a vim shortcut for <name of current file>?,
:!gcc %seems handier.manatwork– manatwork2012-10-22 12:21:19 +00:00Commented Oct 22, 2012 at 12:21 -
1@manatwork That only works if the current file is actually the main program.Bernhard– Bernhard2012-10-22 12:28:21 +00:00Commented Oct 22, 2012 at 12:28
-
can you elaborate on the Makefile strategy please...user1614244– user16142442012-10-22 12:32:02 +00:00Commented Oct 22, 2012 at 12:32
-
Correct. For other cases your second code should be preferred.manatwork– manatwork2012-10-22 12:32:44 +00:00Commented Oct 22, 2012 at 12:32
-
2@user1614244 - step 1) learn to use make, step 2) invoke
:make. Note however that you can setmakeprgto the build tool of your choiceUseless– Useless2012-10-22 14:24:37 +00:00Commented Oct 22, 2012 at 14:24
The canonical way to do this in Vim is to use the compiler configuration setting. Your vim installation almost certainly comes with a compiler plugin for gcc. Type :help compiler in Vim to find out more about how this works.
To associate gcc with c source files, you need something like this in your .vimrc:
au BufEnter *.c compiler gcc
-
8How would you go about invoking the compiler once you have set it?Alex Chamberlain– Alex Chamberlain2012-10-22 22:13:42 +00:00Commented Oct 22, 2012 at 22:13