[LLVMdev] global question

Jean-Daniel Dupas devlists at shadowlab.org
Sat Mar 7 13:01:35 PST 2009


Le 7 mars 09 à 20:10, John Regehr a écrit :

>> Please correct me if I'm wrong, but how can the compiler know, that  
>> x is not
>> initialized in another file which defines x as extern? It can only be
>> sure, when x is declared static.
>
> I don't think you can attach an initializer to an extern except at the
> point where the variable is defined.  But since x is defined here,  
> there
> would then be two definitions of x, a link-time error.
>

FWIW

-------- main.c ----------
#include <stdio.h>

int x;

int main(int arcg, char **argv) {
	fprintf(stderr, "x is: %d\n", x);
	return 0;
}


---------- init.c ---------
extern int x;

__attribute__((constructor))
static void __initialize() {
	x = 12;
}


[MacBook:~/Desktop]% gcc -c init.c
[MacBook:~/Desktop]% gcc -c main.c
[MacBook:~/Desktop]% gcc -o test init.o main.o
[MacBook:~/Desktop]% ./test
x is: 12


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20090307/b1ce8e4a/attachment.html>


More information about the llvm-dev mailing list