question on __attribute__ ((constructor))

Joe Wong joewong@tkodog.no-ip.com
Wed Nov 6 19:26:00 GMT 2002


Hello,

  There is something I really don't understand. Consider the program below:

#include <stdio.h>

int i=0;

static void procinfo_init(void) __attribute__ ((constructor));
static void procinfo_fini(void) __attribute__ ((destructor));

void procinfo_init(void)
{
        printf("HI!\n");
        i = 1;
}

void procinfo_fini(void)
{
        printf("BYE!\n");
}

int main(int argc, char *argv[])
{
        printf("main\n");
        printf("i=%d\n", i); 
}

When I name the program as "a.c" and compile using:
    gcc -g -o a a.c

When I run 'a', I got:
HI!
main
i=1
BYE! 

But, when I name the program as "a.cpp" and using the same compiler flag:
    gcc -g -o a a.cpp 

I run 'a', this time I only get:
main
i=0   

Using 'g++ -g -o a a.cpp' makes no difference. I am using:
gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs
gcc version 2.96 20000731 (Red Hat Linux 7.1 2.96-98)     

So, what could be the problem here?

Thanks!

- Joe



More information about the Gcc mailing list