OxSon / linked_list

0 stars 1 forks source link

tf is a makefile #2

Open OxSon opened 5 years ago

OxSon commented 5 years ago

Should i concern myself with this now or don't worry about it until later?

@4e554c4c

4e554c4c commented 5 years ago

A makefile is a file which describes how a project is to be built.

It is not at all specific to C, you can use it anywhere you need to transform some sort of files into other files in a simple and direct way.

At its core, a make file is a DAG (directed acyclic graph) where each "rule" describes a file that make can produce, and other files which it depends on.

For example, this makefile is very simple and only describes one step, we have one rule

out/main: src/main.c | out

which means the file out/main depends on the file src/main.c and (a special kind of prerequisite called an "order-only" prerequisite since it is somewhat special, in this case a directory that only has to be created once) Then we have the recipie on how to build the file, and that is just

    $(CC) $< -o $@

which really just a confusing way to write "gcc src/main.c -o out/main". The reason I did this is that it is best practice to not hardcode one compiler, so that people can easily compile with another. $< and $@ are just two nice variables which mean "first prerequisite" (src/main.c) and "target" (out/main). These are not necessary to use, but they are very useful when writing rules that do not only apply to one file (i.e. not hardcoding main.c)!

Makefiles are very nice to have, because once you have them you can type make and your project will automatically be recompiled, with only the parts needed recompilation being built. You can call make with a target (i.e. make all or make out/main) but by default the first rule will be run if you don't give any arguments (here it defaults to make all).

I also included the clean target, which deletes the built files.

For an example of a larger makefile, and one not used for a C project, you can look at my OS makefile