This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

attribute ((constructor)) does not work in C++?


Hi,

 I am using Mandrake 9.1 with gcc version 3.2.2 (Mandrake Linux 9.1
3.2.2-3mdk). Compiling the following program ( gcc -g -o c c.cpp )

#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);
}

The output:
[root@test root]# ./c
main
i=0

However, if I rename the source file to ".c" and recompile with "gcc -g -o c
c.c", the output becomes:
[root@test root]# ./c
HI!
main
i=1
BYE!

This time, the constructor and destructor functions are executed. May I know
if this is a feature or a bug? I tried it with gcc 2.96 on RH 7.2, same
result. But with gcc 2.91 on RH 6.2, both program gives me correct result.

Best regards,

- joe


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]