In my C code, there were some instances of non-static symbols with the same name but different types across multiple compilation units. For example,
// file_a.c
#include <stdio.h>
#include <stdint.h>
struct a {
uint32_t value;
} global;
int main()
{
printf("%u\n", global.value);
return 0;
}
and
// file_b.c
#include <stdint.h>
struct b {
uint32_t *value_p;
} global;
Yes i understand this is a stupid program...it's meant to approximate code at a company I work at and cannot post but this is the idea. Compilation like so
gcc -O0 -o executable file_a.c file_b.c
gives no errors. When I enable -flto
gcc -O0 -o executable -flto file_a.c file_b.c
it gives a build error "warning: type of ‘global’ does not match original declaration [-Wlto-type-mismatch]" as expected. As far as I understand, these global variables with the same name are resolved to the same symbol at link time since they are not static. I am not sure what role -flto would play but regardless this coding style can give rise to bugs with or without -flto. For various reasons, I cannot enable -flto but would like to catch these instances as warnings or errors. If I enable the listed flag from the error output:
gcc -O0 -o executable -Wlto-type-mismatch file_a.c file_b.c
it builds, since it seems the flag does nothing unless -flto is enabled. So my question is how can I catch these errors (duplicate names for symbols across compilation units, with or without differing types) and use that flag without enabling -flto. Is it just not an error unless link time optimization is on?
Thank you very much.