This is your simple make file for hello program.

CC      = gcc
CFLAGS  = -g
RM      = rm -f


default: all

all: Hello

Hello: Hello.c
    (CFLAGS) -o Hello Hello.c

clean veryclean:
    $(RM) Hello

Suppose you have two makefiles in one directory named makefile.m1 and makefile.m2 and if you want build both make file then please use following commands

make -f makefile.m1
make -f makefile.m2

or use single Makefile that contains:

m1:
  make -f makefile.m1

m2:
  make -f makefile.m2

and use make m1 or make m2

Now lets clear your doubt about name of make file must not require Makefile

You can name makefile whatever you want. suppose i would like to give name myfirstmakefile.mk. To use it later you need to tell make what makefile you want. Use -f option for this:

make -f myfirstmakefile.mk

And again extantion .mk is also not manadatory you can use whatever you want but never forgot to use -f option.

so may this help make sense to you.

Answer from Jayesh Bhoi on Stack Overflow
🌐
Colby College
cs.colby.edu › maxwell › courses › tutorials › maketutor
A Simple Makefile Tutorial
By putting the object files--hellomake.o and hellofunc.o--in the dependency list and in the rule, make knows it must first compile the .c versions individually, and then build the executable hellomake. Using this form of makefile is sufficient for most small scale projects.
🌐
Makefile Tutorial
makefiletutorial.com
Makefile Tutorial by Example
Typically, when a target is run (aka when the commands of a target are run), the commands will create a file with the same name as the target. In this case, the hello target does not create the hello file. Let's create a more typical Makefile - one that compiles a single C file.
Discussions

unix - How to write a Makefile to compile a simple C program - Stack Overflow
How would a Makefile for this task exactly look? Thanks in advance :) " ... For some reason, low-level programmers DO participate in StackOverflow but think that is not the right place to provide good answers and teach the ones who don't know, only to be cynical and provide links or suggest ... More on stackoverflow.com
🌐 stackoverflow.com
Creating A Basic Make File for Compiling C Code
Sorry to put this bluntly but: oh no, yet another Makefile tutorial that teaches the wrong way to do it. Make is all about using the implicit rules to build object files and using the dependency resolution rules that make things click. People see these clunky ad-hoc Makefile tutorials and think that make is a bad tool. To compile and link a program consisting of a single C file (test.c), this is what your initial Makefile should look like: test: test.o That's it! A single line! Nothing else is required. For compiler arguments, you add CFLAGS. For linker flags you add LDFLAGS and LDLIBS. Don't invent your own names for this, they won't work with the built-in rules. Read the Make manual (seriously!) to find the built-in rules and how they work. When you need automatic dependency resolution (ie. header files), just add -MMD to your CFLAGS (assuming gcc or clang) and add an -include clause to your Makefile to include the dependency files. Here's a more thorough (but untested) example: CFLAGS=-std=c99 CFLAGS+=-MMD # dependency generation magic CFLAGS+=-W -Wall LDLIBS=-lm LDFLAGS= SRCS=myapp.c foo.c bar.c OBJS=$(SRCS:.c=.o) DEPS=$(OBJS:.o=.d) .PHONY: all clean all: myapp myapp: myapp.o foo.o bar.o -include $(DEPS) # this makes magic happen clean: rm -f myapp rm -f $(DEPS) $(OBJS) Notice how that doesn't include a single explicitly written rule for building anything. This is the way (I think) Makefiles should be written from ground up. There's no shortage of tutorials on writing Makefiles by writing your own rules, but fail to tell that Make already knows most of the rules you're going to feed to it. Writing an explicit rule is your first baby step in learning Make but you should not need to do that very often, a good Makefile tutorial should concentrate on the dependency resolution and rule application which is what makes Make an application that's still in use after decades. More on reddit.com
🌐 r/coding
34
50
July 7, 2014
A good makefiles for dummies tutorial. Is there any?
GNU make manual More on reddit.com
🌐 r/C_Programming
8
17
September 25, 2016
Makefiles Tutorial

Step 1. Use CMake.

Step 2. No, really, shut up and just use CMake.

More on reddit.com
🌐 r/programming
6
0
February 28, 2018
🌐
Reddit
reddit.com › r/c_programming › the perfect makefile
r/C_Programming on Reddit: The Perfect Makefile
November 2, 2024 -

(This post is about building C-projects, which is an important part of coding in C. I hope that counts as "on topic" :^) )

When I started coding small C and C++ programs in my free time, I either created imperfect makefiles by blindly copying Stackoverflow answers, or replaced make with other programs such as CMake because I thought make was inadequate.

Now I know a little about make, and find that it is perfectly adequate for small hobby projects, and probably for large ones as well, though I couldn't speak from experience there.

What should the makefile do?

  1. Compile each translation unit if, and only if, it changed or one of the user-defined header files it depends on did

  2. Combine the translation units' object files into an executable, linking with libraries if necessary

  3. Distinguish between compiling 'debug' executables, including debug symbols and assertions, and 'release' executables, without those, which are optimized

  4. Install the executable

Our example

We are looking at a simple program which has two different source files and headers:

main.c:

#include "message.h"

int main(void)
{
    message();

    return 0;
}

message.c:

#include <stdio.h>

#include "message.h"
#include "answer.h"

void message(void)
{
    printf("%s %d\n", MSG, ANSWER);
}

message.h:

#define MSG "The answer is"

void message(void);

answer.h:

#define ANSWER 42

Building object files

First we tell make what compiler to use and how:

CC=gcc
CFLAGS=-MMD -Wall -Wextra -pedantic -std=c11

Then we make a list of all source files and object files we are looking at:

SRC=$(wildcard src/*.c)
OBJ=$(SRC:%.c=%.o)

The first line grabs all files in the folder src that end in .c, and the second makes another list by copying the first and replacing the final .c with .o.

Then we make the rule to compile any given object file:

%.o: %.c
	$(CC) $(CFLAGS) -c -o $@ $<

Source file dependencies

I used to think setting up make so that it would compile a translation unit when one of the included header files changed was too complicated a thing to do, which led me to use CMake for a lot of projects. Turns out, after doing some more research, it is actually incredibly easy.

This ignorance of mine led me to use CMake, which is a turing-complete programming language disguised as a build system, to build programs with six or seven .c-files---effectively aiming a Tsar Bomba at a farm in Missouri. FYI, cloc tells me that CMake (version 3.31.0-rc3) has 291081 lines of code, while GNU make (version 4.4) has 27947. Keep in mind that CMake, after all those lines of code, doesn't even build the project but spits out a makefile itself, which does it.

(That is not to say that you are wrong for using CMake, or that it is not better for large programs. This is about using a small tool for a small task.)

It turns out that the C-compiler can generate a make-compatible list of dependencies for a C-file. That is a program we are already using, and it can do that as a side task while compiling the object file, so we might as well have it do that.

Looking at src/main.c, running the the compiler as follows…

$ gcc -MMD -c -o src/main.o src/main.c

…does not only give me the object file, but also a file called src/main.d, which looks like this:

$ cat src/main.d
src/main.o: src/main.c src/message.h

If you have worked with makefiles before, you'll recognize that is exactly what we'd put into it if we were giving it the dependencies by hand.

Let's first grab a list of all those .d files:

DEP=$(OBJ:%.o=%.d)

Now, before we tell the makefile how to build the object files, we'll tell it to -include $(DEP). include works the same as it does in the C-preprocessor: it treats the content of the given file(s) as if they were typed into the makefile. Prepending a minus to include tells make not to complain if the file(s) do not exist, which would be the case when we are first compiling our project.

Now, after adding a compiler flag, and adding two further lines, our object files are compiled whenever one of their dependencies changes.

(That we get the .d files only after we have compiled the translation unit is fine, because if we change the source file, we need to recompile it that time anyway. If we later change one of the headers, we have the .d file ready.)

Compiling the executable

We add to our makefile's header:

EXE=msg
LIBS=$(addprefix -l,)

If we did need libraries, we would say something like:

LIBS=$(addprefix -l,m pthread)

Then we tell make how to compile msg:

$(EXE): $(OBJ)
	$(CC) -o $@ $^ $(LIBS)

($^, as opposed to $<, expands to all dependencies instead of just the first.)

Other targets

We are done with step one and two, but we still need to distinguish between debug and release builds, and install the executable.

debug: CFLAGS += -g
debug: $(EXE)

The first line says that, if we want to make the target debug, CFLAGS is expanded by the -g flag.

Similarly:

release: CFLAGS += -O3 -DNDEBUG
release: $(EXE)

Since make defaults to the first target, we could either put debug at the top or use the usual default target, all:

all: debug

(Cleaning up)

Sometimes, for example after changing the makefile itself, you want to rebuild the project even though none of the source files have changed. For that we would first introduce a target to get rid of the old output files:

clean:
	rm -f $(OBJ) $(DEP) $(EXE)

Which we can then use to build again from scratch:

remake: clean debug
.NOTPARALLEL: remake

Adding remake to the .NOTPARALLEL pseudo-target tells make not to do clean and debug simultaneously, if something like -j4 was passed. We obviously don't want to start building and then have files deleted.

Since we would usually want to switch to release after having tested the debug build, we can also use clean there:

release: CFLAGS += -O3 -DNDEBUG
release: clean $(EXE)
.NOTPARALLEL: release

Installing

I simply use:

TARGET=/usr/local

install: all
	cp $(EXE) $(TARGET)/bin

You could also make it depend on release but that would rebuild an executable you probably just built. This way the usual paradigm of…

$ make release
$ sudo make install

…is followed, but that is simply a matter of preference.

Conclusion

The final makefile looks like this:

CC=gcc
CFLAGS=-MMD -Wall -Wextra -pedantic -std=c11

SRC=$(wildcard src/*.c)
OBJ=$(SRC:%.c=%.o)
DEP=$(OBJ:%.o=%.d)

EXE=msg
LIBS=$(addprefix -l,)

TARGET=/usr/local

all: debug

debug: CFLAGS += -g
debug: $(EXE)

remake: clean debug
.NOTPARALLEL: remake

release: CFLAGS += -O3 -DNDEBUG
release: clean $(EXE)
.NOTPARALLEL: release

clean:
	rm -f $(OBJ) $(DEP) $(EXE)

install: all
	cp $(EXE) $(TARGET)/bin

$(EXE): $(OBJ)
	$(CC) -o $@ $^ $(LIBS)

-include $(DEP)

%.o: %.c
	$(CC) $(CFLAGS) -c -o $@ $<

It can be used like this:

$ make
gcc -MMD -Wall -Wextra -pedantic -std=c11 -g -c -o src/main.o src/main.c
gcc -MMD -Wall -Wextra -pedantic -std=c11 -g -c -o src/message.o src/message.c
gcc -o msg src/main.o src/message.o
$ touch src/answer.h
$ make
gcc -MMD -Wall -Wextra -pedantic -std=c11 -g -c -o src/message.o src/message.c
gcc -o msg src/main.o src/message.o
$ ./msg
The answer is 42

So we solved not only building C-projects but also 'calculated' the Answer to the Ultimate Question of Life, the Universe, and Everything. If you happen to write a program to calculate the Ultimate Question, though, I'm afraid you'd need CMake.

Top answer
1 of 4
28

This is your simple make file for hello program.

CC      = gcc
CFLAGS  = -g
RM      = rm -f


default: all

all: Hello

Hello: Hello.c
    (CFLAGS) -o Hello Hello.c

clean veryclean:
    $(RM) Hello

Suppose you have two makefiles in one directory named makefile.m1 and makefile.m2 and if you want build both make file then please use following commands

make -f makefile.m1
make -f makefile.m2

or use single Makefile that contains:

m1:
  make -f makefile.m1

m2:
  make -f makefile.m2

and use make m1 or make m2

Now lets clear your doubt about name of make file must not require Makefile

You can name makefile whatever you want. suppose i would like to give name myfirstmakefile.mk. To use it later you need to tell make what makefile you want. Use -f option for this:

make -f myfirstmakefile.mk

And again extantion .mk is also not manadatory you can use whatever you want but never forgot to use -f option.

so may this help make sense to you.

2 of 4
12

A makefile is a recipe for the make utility how to create some file (called a target) from some other files (called dependencies) using a set of commands run by the shell. A makefile typically looks like this:

target: dependency [...]
        command1
        command2

Try running man make for details.

Now for your task, really there is no need for a Makefile, since make has built-in rules that know how to compile a simple program. All you need to do is place your C source in a file named after the executable name (Hello) and with a .c extension, i.e. Hello.c.

Then a simple

$ make Hello
cc     Hello.c   -o Hello

does everything. If you want to use gcc instead of cc, you can run

$ rm Hello
$ make CC=gcc Hello
gcc     Hello.c   -o Hello

If you tell your instructor/teacher/prof that an empty makefile is all you need since you know the built-in rules do the right thing, you'll get some extra credit and maybe your instructor has learnt something new :-) If you are asked for a reference, you could quote the relevant parts of the make manual, or, do it like a pro, quote from the POSIX Standard for the make utility, section Default Rules.

🌐
Usask
cs.usask.ca › staff › oster › makefiles.html
A Brief Introduction to Makefiles
January 7, 2024 - For this Makefile, the result of doing a 'make' will be the construction of 'myprogram'. The lines: myprogram: main.o part1.o part2.o gcc -o myprogram main.o part1.o part2.o indicate that three pieces: main.o, part1.o, and part2.o are required to construct 'myprogram'.
🌐
Stanford
web.stanford.edu › class › archive › cs › cs107 › cs107.1174 › guide_make.html
CS107 Guide to makefiles
The command make invokes the make program which reads in the file Makefile from the current directory and executes the build commands necessary to build the default target. You can also name just the specific target you want to build, such as make reassemble or make myprogram.
Find elsewhere
🌐
Johnatten
johnatten.com › 2014 › 07 › 06 › creating-a-basic-make-file-for-compiling-c-code
Creating A Basic Make File for Compiling C Code | John Atten
July 6, 2014 - Of course, we need to be in the directory in which the make file and the hello.c source file are located. Of course, compiling our Hello World application still represents a pretty simplistic view of the compilation process. We might want to avail ourselves of the Make utilities strengths, and cook up a more general template we can use to create make files. The Make utility allows us to structure a makefile in such a way as to separate the compilation targets (the source to be compiled) from the commands, and the compiler flags/arguments (called rules in a make file).
🌐
HowStuffWorks
computer.howstuffworks.com › tech › computer software › programming
Makefiles - The Basics of C Programming | HowStuffWorks
March 8, 2023 - You can use the following makefile to replace the compilation sequence above: main: main.o util.o gcc -o main main.o util.o main.o: main.c util.h gcc -c -g main.c util.o: util.c util.h gcc -c -g util.c · Enter this into a file named makefile, and type maketo build the executable.
🌐
Reddit
reddit.com › r/coding › creating a basic make file for compiling c code
r/coding on Reddit: Creating A Basic Make File for Compiling C Code
July 7, 2014 - CFLAGS=-std=c99 CFLAGS+=-MMD # dependency generation magic CFLAGS+=-W -Wall LDLIBS=-lm LDFLAGS= SRCS=myapp.c foo.c bar.c OBJS=$(SRCS:.c=.o) DEPS=$(OBJS:.o=.d) .PHONY: all clean all: myapp myapp: myapp.o foo.o bar.o -include $(DEPS) # this makes magic happen clean: rm -f myapp rm -f $(DEPS) $(OBJS) Notice how that doesn't include a single explicitly written rule for building anything. This is the way (I think) Makefiles should be written from ground up.
🌐
Medium
medium.com › @zackbunch › how-to-write-a-make-file-for-c-c-73901e2f5ab1
How to write a make file for C/C++? | by Zack Bunch | Medium
January 3, 2022 - Make is a tool that controls the ... A Makefile is a file that is used to describe the steps needed to build and install our code without the end-user ever having to know the details of how it is done....
🌐
GNU
gnu.org › software › make › manual › make.html
GNU make
You can use it to describe any task where some files must be updated automatically from others whenever the others change. ... To prepare to use make, you must write a file called the makefile that describes the relationships among files in your program and provides commands for updating each file.
🌐
Colorado State University
cs.colostate.edu › ~cs157 › LectureMakefile.pdf pdf
1 Makefiles, and .h files, and .c files, and .o files, OH MY!
~cs157/class/7_Makefile · – main.c · • The main function, to actually do the “job” · – stack.c · • The code for a stack of integers. – stack.h · • The “declarations” of a stack of integers. 4 · Why break them up? • main just needs a stack ·
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › makefile-in-c-and-its-applications
MakeFile in C++ and its applications - GeeksforGeeks
July 25, 2024 - STEP 1: Create a file with name "makefile" and open it with any text editor. STEP 2: Inside the makefile, define the variables for compiler name, flags, target, source and object files.
🌐
Quora
quora.com › What-is-a-makefile-in-C-programming
What is a makefile in C programming? - Quora
Answer (1 of 3): C is designed around the idea that each file gets compiled separately, and then you link them all together (with all the pre-compiled libraries you’re using) into a program. Like this: [code]cc foo.c cc bar.c cc baz.c ld -o foobar foo.o bar.o baz.o -lc -lmath [/code]In fact, us...
🌐
Swarthmore College
cs.swarthmore.edu › ~newhall › unixhelp › howto_makefiles.html
Using make and writing Makefiles
Create a Makefile listing the rules for building the executable the file should be named 'Makefile' or 'makefile'. This only has to be done once, except when new modules are added to the program, the Makefile must be updated to add new module dependencies to existing rules and to add new rules to build the new modules. After editing program file(s), rebuild the executable by typing make: % make A specific target in the Makefile can be executed by typing: % make target_label For example, to execute the rm commands in the example makefile below, type: % make clean
🌐
DEV Community
dev.to › deciduously › how-to-make-a-makefile-1dep
How To Make A Makefile - DEV Community
July 25, 2019 - A great number of *nix packages are distributed as C or C++ source code, and will be built something like this. The first line runs a separate program to configure your Makefile for you, which is necessary in big projects which rely on system libraries. The last line generally assumes admin rights so it can copy the executable(s) it just built onto the system path.
🌐
HackerNoon
hackernoon.com › how-to-create-a-library-in-c-with-a-makefile
How to Create a Library in C with a Makefile | HackerNoon
August 27, 2023 - Hello everyone, I’d like to discuss creating a library using a Makefile in C with some practical examples.
🌐
Clemson University
people.computing.clemson.edu › ~dhouse › courses › 1070 › labs › 9-9 › makefile-tutorial.html
A Simple Makefile Tutorial
In the example below, all of the include files should be listed as part of the macro DEPS, and all of the object files should be listed as part of the macro OBJ. CC = gcc CFLAGS = -g DEPS = hellomake.h OBJ = hellomake.o hellofunc.o %.o: %.c $(DEPS) $(CC) $(CFLAGS) -c -o $@ $< hellomake: $(OBJ) $(CC) $(CFLAGS) -o $@ $^ So, now let us look at a makefile that you could use for your EZ Draw projects.
🌐
Avikdas
avikdas.com › 2019 › 12 › 16 › makefiles-for-c-cpp-projects.html
Makefiles for C/C++ projects
December 16, 2019 - In this case, that means you need main.c to exist, so it can produce main.o, which then matches the specified output of main. Again, the CC, LDFLAGS and LDLIBS variables will be honored. This leaves our final Makefile with mostly project-specific configuration and very little boilerplate:
🌐
GeeksforGeeks
geeksforgeeks.org › c language › how-to-use-make-utility-to-build-c-projects
How to use make utility to build C projects? - GeeksforGeeks
April 4, 2026 - In C/C++ development, code is split ... into an object file first, your app cannot be built. The make utility automates this by following a script called a Makefile....