-Pre-condition:
I know it has many ways to ignore this warning, but I do need fix it but NOT just add some flags for compiler to ignore it, because I can do this makefile CFLAG
modification on my local, but the compile/build policy can NOT been changed due to company quality control reason. This question I just want to discus who to fix it in a good way but NOT ignore them, many thanks!
Meanwhile, not ONLY some variables are never referenced, but also it has warning:
function 'xxx' was declared but never referenced
the similar problem in head file, it has some static functions, which ONLY use in one c file but many others c files include this head file.
Furthermore, this code run on a dedicate target, which has critical memory consumption, that means I need take care of each bit both in ROM and RAM.
-------Problem shows in below-----------
I currently build some codes, which provide by vendor and it contains lots of warning, because I want to have a warning free build, therefore, I put -Werror
for gcc
and --diag_error=[error_ref_number]
for armcc
.
After above makefile modification, the most warning I currently meet is
variable 'xxx' was declared but never referenced
It caused by following coding style:
in veryBIG.h
, it defined some variables like (just for example but the vendor's code has exactly the same way):
static const int a = 10;
static const int b = 20;
static const int c = 30;
...
static const int z = xx;
however, many c files include this BIG head file, but ONLY one of these c file use one of above variables, it like a.c
will use variable a
, b.c
use variable b
, etc,.
I have two ways to fix:
move these kind of variables into it dedicate c file, and it means head file become useless
separate head files, means I create a.h
, b.h
, ..., z.h
, and each dedicate c file include ONLY one of above head file
However, both of these two ways have limitation for my work,
Way 1:
I'm NOT sure if vendor further update will change this head file to what or have some update values, because vendor do NOT want to fix this compile warnings, that means if I follow way 1, I will manually update these variables if it has been changed by vendor (ONLY in head file, and I must synchronize them to c file) and this way makes my further merge work become complicated
Way 2:
It also makes my further merge work become NOT easy, because I do NOT use the vendor way and I also need modify the system makefile to adapt these head files into INC
PATH if they are NOT locate in same folder (which the PATH already define for that veryBIG.h
)
If there any idea to overcome this problem?
UPDATE
I can temporary use Wno-xxx
for gcc
and delete some [error_ref_number]
in flag --diag_error
(like CFLAG += --diag_error=550,223,188,177,....,
, and I delete 177
for pass this warning in armcc
) and makes my compile go on and first fix others warnings, but I do need fix ALL warning.
If you care about memory usage to the point where every bit matters, then you probably should break up that header file into many small pieces, because there is no actual guarantee that the compiler won't emit every single one of those static
variables and functions over and over in every single .c
file even though only a few of them are used. (The as-if rule says the compiler may delete all the unused statics, but nothing says it has to.)
In your shoes I would probably write some sort of script which would automatically do the break-up; then I wouldn't have to do it all over again when new releases happened.