This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
global object constructors and constructor function priorities
- From: Maxim Yegorushkin <maxim dot yegorushkin at gmail dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Fri, 18 May 2012 13:21:43 +0100
- Subject: global object constructors and constructor function priorities
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