Skip to content

Commit 187d6ab

Browse files
authored
Partial fix for #13875 (Document preprocessorErrorDirective) [ci skip] (danmar#7542)
1 parent af9fb50 commit 187d6ab

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
# preprocessorErrorDirective
3+
4+
**Message**: #error message<br/>
5+
**Category**: Configuration<br/>
6+
**Severity**: Error<br/>
7+
**Language**: C and C++
8+
9+
## Description
10+
11+
The `#error` directive is a preprocessor instruction in C and C++ that explicitly generates a compilation error. It is typically used as a safeguard to prevent compilation under incorrect conditions—like unsupported configurations, platforms, or missing defines.
12+
13+
These warnings from Cppcheck do not indicate a bug in your code. These warnings indicate that the Cppcheck configuration is not working.
14+
15+
## How to fix
16+
17+
The warning is typically reported for an `#error` directive that is located inside some conditional preprocessor block (`#if..`, `#else`, etc):
18+
```cpp
19+
#ifndef __BYTE_ORDER__
20+
#error Byte order is not defined
21+
#endif
22+
```
23+
24+
The code here is correct and you should not try to change it.
25+
26+
Somehow it will be necessary to define `__BYTE_ORDER__` in Cppcheck analysis.
27+
28+
### gcc compiler macro
29+
If you compile your code with gcc and the macro is provided by gcc, then you can define all gcc-macros using these commands:
30+
```
31+
echo x > dummy.c
32+
gcc -dM -E dummy.c > gcc-macros.h
33+
```
34+
The gcc-macros.h that is generated can be included in cppcheck using the `--include` option:
35+
```
36+
cppcheck --include=gcc-macros.h ....
37+
```
38+
39+
### library macro
40+
If the macro that is needed is defined in some library header it might be possible to fix the issue by using an extra `--library` option:
41+
```
42+
cppcheck --library=foo .....
43+
```
44+
45+
### manually defined macro
46+
To define extra macros manually you can use `-D`:
47+
```
48+
cppcheck -D__BYTE_ORDER__=123 .....
49+
```
50+
51+
### use --force or --max-configs
52+
You can let Cppcheck try to resolve the required defines:
53+
```
54+
cppcheck --force .....
55+
```

0 commit comments

Comments
 (0)