summaryrefslogtreecommitdiff
path: root/md/writeup/makefile_tips.md
blob: e9c8077ac75a119d892e02d4d6dd34cff2e6029e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
title:Makefile tips
keywords:makefile

# Makefile tips
Makefile working tips. Usual simple makefile
looks like this:

```Makefile
make:
    gcc main.c -o main
```

but when your project grows and you use more files it becomes
like this:

```Makefile
make:
    gcc -O2 -c file1.c
    gcc -O2 -c file2.c
    gcc -O2 -c file3.c
    gcc -O2 main.c file1.o file2.o file3.o -o main
```

This is standard growth of file. Its better practice to
reuse your code, it could applied to your makefile also.

When you have few small project and you want simply copy
makefile and it works.

## Compiler name
Usually everyone prefer one compiler. And time to time only
check if code compiles with other compilers.

```Makefile
CC=gcc
make:
    $(CC) -O2 -c file1.c
    $(CC) -O2 -c file2.c
    $(CC) -O2 -c file3.c
    $(CC) -O2 main.c file1.o file2.o file3.o -o main
```

## Project name
Every project have unique name and you also would like
to change it if there is need.

```Makefile
PROJECT=project
CC=gcc
make:
    $(CC) -O2 -c file1.c
    $(CC) -O2 -c file2.c
    $(CC) -O2 -c file3.c
    $(CC) -O2 main.c file1.o file2.o file3.o -o $(PROJECT)
```

## Compiler flags
Usual problem is when some compiling flags causes problems
and you need to change every single entry in file.

```Makefile
PROJECT=project
CC=gcc
CFLAGS=-O2
make:
    $(CC) $(CFLAGS) -c file1.c
    $(CC) $(CFLAGS) -c file2.c
    $(CC) $(CFLAGS) -c file3.c
    $(CC) $(CFLAGS) main.c file1.o file2.o file3.o -o $(PROJECT)
```

This 3 variables reduce how number of places that you need
to edit if something changes.

Next thing is to fix problem when you need to add or remove
file from project and after that you have manually to edit
at least 2 lined in makefile.

## Use multiple files

One of they ways how to reduce number of files edited is to add
new variable where all files is listed:

```Makefile
PROJECT=project
CC=gcc
CFLAGS=-O2
SOURCES=file1.c file2.c file3.c
OBJECTS=$(SOURCES:.c=.o)
 
all: $(OBJECTS) $(PROJECT)
 
$(PROJECT): $(SOURCES) main.c
    $(CC) $(OBJECTS) $(CFLAGS) main.c -o $(PROJECT)
 
%.o: %.c
    $(CC) $(CFLAGS) -c $<
```

Here was added file auto-matching for *.c files to make them *.o

## Auto matching C files

Last thing to add is auto match all *.c in directory.

```Makefile
PROJECT=project
CC=gcc
CFLAGS=-O2
SOURCES=$(wildcard *.c)
OBJECTS=$(SOURCES:.c=.o)
 
all: $(OBJECTS) $(PROJECT)
 
$(PROJECT): $(SOURCES) main.c
    $(CC) $(OBJECTS) $(CFLAGS) -o $(PROJECT)
 
%.o: %.c
    $(CC) $(CFLAGS) -c $<
```

Now project makefile can be easily copied and with
changing only one variable value everything should be OK
To run any makefile:

```sh
make -f makefile_name.mk
```



## Downloads
makefile_tips.zip
3KiB - http://archive.main.lv/files/writeup/makefile_tips/makefile_tips.zip