global object constructors and constructor function priorities

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I am reading http://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Function-Attributes.html#Function-Attributes
where it talks about constructor and destructor function attributes:

    The priorities for constructor and destructor functions are the
same as those specified for namespace-scope C++ objects.

In one translation unit constructors of C++ global objects are called
in the order of object definition. So, I thought what the above quote
says is that global object constructors and constructor functions have
the same priority and are called in the order of their definition. I
made a tiny program to test it:

    #include <stdio.h>

    void foo() __attribute__((constructor));

    struct X {
        X() { printf("%s\n", __PRETTY_FUNCTION__); }
        ~X() { printf("%s\n", __PRETTY_FUNCTION__); }
    } x;

    void foo() { printf("%s\n", __PRETTY_FUNCTION__); }

    void bar() __attribute__((constructor));
    void bar() { printf("%s\n", __PRETTY_FUNCTION__); }

    int main() {
    }

Which I compile as:

    $ g++ --version
    g++47 (GCC) 4.7.0
    Copyright (C) 2012 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    $ g++ -o test -std=gnu++11 -Wall -Wextra -g -march=native test.cc


When I invoke it constructor functions always run before C++ global
object constructors, regardless of the order of declaration or
definition:

    $ ./test
    void foo()
    void bar()
    X::X()
    X::~X()

Could anybody clarify whether this behaviour is in agreement with what
gcc constructor function attribute documentation states please?

-- 
Maxim


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux