Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the simplest MAKEFILE I could have the works for a single C file?

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?

like image 752
Ash Avatar asked Nov 28 '10 09:11

Ash


People also ask

What is makefile in C?

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.

What is the extension of makefile in C?

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.


2 Answers

all:
     gcc -o blah main.c

You don't need makefile here, simple shell script is OK.

like image 178
Mariy Avatar answered Oct 02 '22 12:10

Mariy


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

like image 43
Jander Avatar answered Oct 02 '22 14:10

Jander