A Makefile is a file, read by the
Make program, which executes all the
commands and rules in the Makefile.
Remember the first two days of C Pool. You were supposed to use bash and
build your own aliases and scripts.
Make is kind of a custom build
script that makes your programmer-life really easier.
At EPITECH, you’ll be using Makefiles to compile your code, using a
A simple Makefile is composed of your project source files (the .c files) and some
rules to make your
Make command work.
You have to list your source files like this:
SRC = ./main.c \ ./file.c
After that, you can use them to build your objects. It will take all
$(SRC) and compile them into
OBJ = $(SRC:.c=.o)
For the compilation there is a feature that allow you to compile each
.c with flags, it’s the
For example let’s add verification of errors flags :
-Werror -Wextra and a flags to find
.h of your project :
You can call this variable
CFLAGS for compilation’s flags.
CFLAGS += -Werror -Wextra -I./include
Be careful !
You don’t have to call this variable in your Makefile, he will solo add it to the compilation of your
Now, set the name of your final binary using
NAME, so the AutoGrader can
find your binary correctly.
NAME = binary_name
Then, it is mandatory to create a $(NAME) rule that will execute other rules, and render a binary.
$(NAME): $(OBJ) gcc -o $(NAME) $(OBJ) all: $(NAME)
When you have a rule like
$(NAME), the rules put in the same line will be used as mandatory rules.
After those rules have been called, the command on the next lines will be called.
For instance, this will execute the
ls command without executing any previous rule.
You can also have some rules that permit you to clean-up your folder without having to do it manually.
For instance, this
clean will remove
fclean will remove
.o files and the binary.
re will do
fclean and re-make your binary.
clean: rm -f $(OBJ) fclean: clean rm -f $(NAME) re: fclean all
Don’t forget to put a
.PHONY, in order to avoid relinking. Put all the rules you use.
.PHONY: all clean fclean re
And that’s pretty much it ! Your Makefile is now ready to use.
At EPITECH, you use
criterion for unit tests. In order to make it clean there is a approach given by EPITECH.
First of all, you have to add a new rule to your main Makefile, according to EPITECH this rule should be named
In order to make it cleaner we recommend you to another Makefile in the tests directory and link to the main. To call a Makefile rule of your tests Makefile just type : make -C tests/ [rule_name] in your main Makefile.
tests_run rule should compile your sources files
.c and your tests files. This rule must launch your binary
You never have to put your main function in the source files that you compile for unit tests : Criterion have his own.
Your tests must compile with the CFLAG
--coverage (see Generic Makefile). This flag will create
.gcno of your sources files.
Make a rule to clean all your
Now, when you launch your tests_run rule, your binary of tests should compile again and execute so that you can see if you passed tough your tests. You should see your files from –coverage. You can use the gcov [files] to see how many line were executed when you launch your unit tests.
.c.gcov and you can push it to the AutoGrader !