Do weak variables waste mem?

Alan Modra amodra@gmail.com
Wed Feb 25 19:07:00 GMT 2015


On Tue, Feb 24, 2015 at 01:01:15PM +0000, Thomas Schmid wrote:
> For me it looks like a waste of mem. Why is there space left on the end of section .bss?

You have defined two copies of the variables.  Without the
__attribute__((__weak__)) you also have two definitions of each
variable, but some compilers (including gcc) treat them specially - as
common variables.  You should instead declare the variables as extern
in the header, and define them in one of the source files.

> <inc.h:>
> 
> int var1 __attribute__((__weak__));
> int var2 __attribute__((__weak__));
> 
> <s1.c>
> #include "inc.h"
> 
> <s2.c>
> #include "inc.h"

-- 
Alan Modra
Australia Development Lab, IBM



More information about the Binutils mailing list