forked from cpputest/cpputest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README_CppUTest_for_C.txt
68 lines (48 loc) · 2.27 KB
/
README_CppUTest_for_C.txt
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
CPPUTest can and has been used for testing C code also. When testing
C code there are a couple of things to keep in mind and a couple of
common problems to solve.
---++ Using extern "C"
When including C-header files or when declaring C-variables and routines
in a .cpp file, you'll have to surround them with an extern "C". This is
because the C++ linker works different than the C linker and you need to
instruct the compiler about this. If you do NOT do this, you will probably
get a linker error, like unresolved symbols, for a routine that you did
implement.
An example:
extern "C" {
#include "hello.h"
extern HelloWorldApi theRealHelloWorldApi;
}
---++ CppUTest support for C
CppUTest comes with a file called TestHarness_c.h which contains a couple
of routines that can be used in C code, like C-versions of the CHECK-MARCO's.
The file also contains malloc and free routines that can be used for using
the CppUTest memory leak detector. These routines should be used instead of
the normal malloc/free. This can be achieved by #defining them somewhere, for
examples as a compiler option: -Dmalloc=cpputest_malloc.
It's important to remember that TestHarness_c.h is a C-header file. It can be
used in C code, but when using in C++ code, you need to use extern "C" before
including it.
---++ C++ keywords used
It sometimes happens that a C file uses a C++ keyword as a type or something
else. The most common one is the bool-type. This can typically be solved by
#defining the bool to something else. For example:
extern "C" {
#define bool helloBool
#include "hello.h"
#undef bool
}
The #undef is optional. It is possible that this solution leads to problems in
some situation (never happened to me). The same solution works for other C++
key-words
---++ Other
* In C, sometimes people use empty structs. The sizeof(empty struct) would be
0. In C++, the sizeof(empty struct) would be something. The best way to fix
this is to not use empty structs in C.
According to http://www.glenmccl.com/bett.htm an empty stuct in C is illegal
anyway.
---++ References
* http://www.glenmccl.com/bett.htm
Describes some differences between C and C++
* http://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B
Wikipedia entry on the compatibility between C and C++