I have a file called "main.c". Whats the simplest Makefile I can have to compile this file into an executable that I can run like ./blah
?
Makefile is a set of commands (similar to terminal commands) with variable names and targets to create object file and to remove them. In a single make file we can create multiple targets to compile and to remove object, binary files. You can compile your project (program) any number of times by using Makefile.
Well on a Linux machine it usually doesn't have an extension at all - it's just "makefile" or "Makefile". You can call it anything you want and use the -f option to tell "make" which to use. If your text editor can't save a file called Makefile , use a better text editor.
all:
gcc -o blah main.c
You don't need makefile here, simple shell script is OK.
If you're running GNU Make and if you don't need to link in extra libraries, the simplest makefile is no makefile at all. Try:
make main
If you don't want to have to specify main
, then you can use the following one-line Makefile:
all: main
GNU Make has several implicit rules that it uses if you don't define them yourself. The one that makes this work is something like:
%: %.c
$(CC) $^ -o $@
For more info, see: http://www.gnu.org/software/make/manual/make.html#Using-Implicit
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With