[LLVMdev] global question

Lennart Augustsson lennart at augustsson.net
Sat Mar 7 16:57:52 PST 2009


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
>
>




More information about the llvm-dev mailing list