-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathcpp.c
118 lines (86 loc) · 2.88 KB
/
cpp.c
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
/*
# cpp
GNU cpp extensions to the ANSI C preprocessor.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
/*
# Predefined macros
# Preprocessor defines
Full list: http://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html#Common-Predefined-Macros
View all macros that would be automatically defined:
cpp -dM /dev/null
TODO there are some missing! where is `__i386__` documented for example?
*/
{
/*
# __COUNTER__
Increments each time it gets used.
Can be used to generate unique numbers.
*/
{
assert(__COUNTER__ == 0);
assert(__COUNTER__ == 1);
assert(__COUNTER__ == 2);
}
/*
# __TIMESTAMP__
*/
{
printf("__TIMESTAMP__ = %s\n", __TIMESTAMP__);
}
/*
# GCC version numbers
`__GNUC__` : major
`__GNUC_MINOR__` : minor
`__GNUC_PATCHLEVEL__`: patch
http://stackoverflow.com/questions/259248/how-do-i-test-the-current-version-of-gcc
There is also:
__GNUC_PREREQ(4,8)
to test versions more conveniently than:
#if defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8
in `<features.h>`.
# Detect if in GCC
#ifdef __GNUC__ is a good common way.
*/
{
#ifdef __GNUC__
printf("__GUNC__ = %d\n", __GNUC__);
printf("__GUNC_MINOR__ = %d\n", __GNUC_MINOR__);
printf("__GUNC_PATCH = %d\n", __GNUC_PATCHLEVEL__);
#endif
/*
Automatically defined if the compiler is told to use strict ansi c features and no extensions
this is triggered by options such as `-std=c99` or `-ansi`.
Don't be surprised if this does not appear when compiling this file
since strict ansi compliance would mean other features of this file would need
to be broken such as nested functions.
*/
#ifdef __STRICT_ANSI__
puts("__STRICT_ANSI__");
#endif
#ifdef __i386__
/*
# Architecture detection macro
http://stackoverflow.com/questions/152016/detecting-cpu-architecture-compile-time
GCC defines architecture macros TODO where?
Seems to use the list: http://sourceforge.net/p/predef/wiki/Architectures/
*/
puts("__i386__");
#elif __x86_64__
puts("__x86_64__");
#endif
#ifdef __linux__
/*
# OS detection macro
http://stackoverflow.com/questions/142508/how-do-i-check-os-with-a-preprocessor-directive
Same riddle as architecture detection.
*/
puts("__linux__");
#endif
}
}
return EXIT_SUCCESS;
}