-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·80 lines (51 loc) · 1.53 KB
/
main.cpp
File metadata and controls
executable file
·80 lines (51 loc) · 1.53 KB
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
/*
preprocessor is a separate program that runs before the compiler when you
compile your program. It’s purpose is to process directives. Directives are
specific instructions that start with a # symbol and end with a newline (NOT a
semicolon).
There are several different types of directives:
1)Includes
2)Macro defines
3)Conditional compilation
4)Header guards
The preprocessor manipulates text before the compiler gets to it.
For example, consider the following program:
add.h:
#include "mymath.h"
int add(int x, int y);
subtract.h:
#include "mymath.h"
int subtract(int x, int y);
main.cpp:
#include "add.h"
#include "subtract.h"
Consequently, all the contents of mymath.h will have been included twice in
main.cpp, which will cause the compiler to complain.
solution:
#ifndef SOME_UNIQUE_NAME_HERE
#define SOME_UNIQUE_NAME_HERE
// your declarations here
#endif
for example in our case:
add.h
#ifndef ADD_H
#define ADD_H
// your declarations here
#endif
subtract.h
#ifndef SUBTRACT_H
#define SUBTRACT_H
// your declarations here
#endif
and because in math.h we have
#ifndef MATH_H
#define MATH_H
// your declarations here
#endif
so after we include add.h, via add.h automatically we define MATH_H, so when we
include subtract.h, MATH_H already defined so #ifndef MATH_H would return false
and the content of math.h won't included again
*/
#include "add.h"
#include "subtract.h"
int main() {}