> 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.> (I even remember someone, that it is platform specific whether x is > initialized to 0 or not... or was it a question of C89 or C99?)Definitely initialized to zero. John
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>
On Sat, Mar 7, 2009 at 11:10 AM, John Regehr <regehr at cs.utah.edu> wrote:> 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.Strictly, that's true, but gcc supports it as an extension. Variables like "int x;" are given common linkage, which allows them to be overridden. -Eli
Or just have init.c say int x = 12; (Works with most C compilers.) On Sat, Mar 7, 2009 at 10:01 PM, Jean-Daniel Dupas <devlists at shadowlab.org> wrote:> > 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 > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > >