GCC optimization bug on weak const variable
问题 I'm getting a weird gcc behaviour when dealing with weak const variables on different optimize levels (i.e. -O0 or -O1 ). Here is the code: def.h : declarations const int var; int copy; int do_copy(void); weak.c : weak var definition, do_copy implementation doing copy = var #include "def.h" const int __attribute__((weak)) var = 1; int do_copy(void) { copy = var; return var; } main.c : strong var definition, and use of do_copy #include <stdio.h> #include "def.h" int copy = 0; int copy2 = 0;