Forward declaration of a struct in .h is insufficient if there is code that accesses the struct within the .h file depending on optimization levels. GCC of Ubuntu 22.04 with -O0 as well as -O3 ends up in these pair of errors: make[1]: Entering directory 'perfbook/CodeSamples/datastruct/Issaquah' cc -g -O0 -Wall -o existence_test existence.c existence_test.c -lpthread -lurcu -lurcu-common -lurcu-signal In file included from /usr/include/x86_64-linux-gnu/urcu/pointer.h:39, from /usr/include/x86_64-linux-gnu/urcu-pointer.h:1, from /usr/include/x86_64-linux-gnu/urcu/rculist.h:30, from ../../api.h:847, from existence.c:30: existence.h: In function ‘existence_exists_relaxed’: existence.h:54:32: error: invalid use of undefined type ‘struct existence’ 54 | struct existence *ep = rcu_dereference(*epp); | ^~~~~~~~~~~~~~~ In file included from /usr/include/x86_64-linux-gnu/urcu/pointer.h:39, from /usr/include/x86_64-linux-gnu/urcu-pointer.h:1, from /usr/include/x86_64-linux-gnu/urcu/rculist.h:30, from ../../api.h:847, from existence_test.c:26: existence.h: In function ‘existence_exists_relaxed’: existence.h:54:32: error: invalid use of undefined type ‘struct existence’ 54 | struct existence *ep = rcu_dereference(*epp); | ^~~~~~~~~~~~~~~ make[1]: *** [Makefile:49: existence_test] Error 1 make[1]: Leaving directory 'perfbook/CodeSamples/datastruct/Issaquah' Move the declaration from .c to .h. Signed-off-by: Akira Yokosawa <akiyks@xxxxxxxxx> --- CodeSamples/datastruct/Issaquah/existence.c | 6 ------ CodeSamples/datastruct/Issaquah/existence.h | 7 ++++++- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/CodeSamples/datastruct/Issaquah/existence.c b/CodeSamples/datastruct/Issaquah/existence.c index 5e9ee70e..2d56a42d 100644 --- a/CodeSamples/datastruct/Issaquah/existence.c +++ b/CodeSamples/datastruct/Issaquah/existence.c @@ -37,12 +37,6 @@ /* Existence-switch array. */ const int existence_array[4] = { 1, 0, 0, 1 }; -/* Existence structure associated with each moving structure. */ -struct existence { - const int **existence_switch; - int offset; -}; - /* Existence-group structure associated with multi-structure change. */ struct existence_group { struct existence outgoing; diff --git a/CodeSamples/datastruct/Issaquah/existence.h b/CodeSamples/datastruct/Issaquah/existence.h index 881c908f..c39a8d03 100644 --- a/CodeSamples/datastruct/Issaquah/existence.h +++ b/CodeSamples/datastruct/Issaquah/existence.h @@ -22,7 +22,12 @@ #ifndef __EXISTENCE_H #define __EXISTENCE_H -struct existence; +/* Existence structure associated with each moving structure. */ +struct existence { + const int **existence_switch; + int offset; +}; + struct existence_group; void existence_wire_call_rcu(void); -- 2.34.1