Bugzilla will be upgraded to version 5.0 on a still to be determined date in the near future. The original upgrade date has been delayed.
Bug 71490 - __attribute__ ((constructor)) doesn't work with g++
__attribute__ ((constructor)) doesn't work with g++
Product: Red Hat Linux
Classification: Retired
Component: gcc (Show other bugs)
i386 Linux
medium Severity medium
: ---
: ---
Assigned To: Jakub Jelinek
Brian Brock
Depends On:
  Show dependency treegraph
Reported: 2002-08-14 06:48 EDT by Wayne Price
Modified: 2007-04-18 12:45 EDT (History)
0 users

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2004-10-02 16:53:05 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)

  None (edit)
Description Wayne Price 2002-08-14 06:48:54 EDT
From Bugzilla Helper:
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 95)

Description of problem:
Code which uses the __attribute__((constructor)) fails to work properly with g++

Version-Release number of selected component (if applicable):

How reproducible:

Steps to Reproduce:
1. Compile the below code using g++
2. Run the resulting program


Actual Results:  Using g++, the program prints the value 0, indicating that the 
constructor function was NOT called. Compiling against gcc shows the correct 

The above code sample has been tested with RedHat releases 6.2, 7.1, 7.2 and 
7.3. Only version 6.2 shows the correct behaviour.

Additional info:

gcc versions:
RedHat 6.2: gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)
RedHat 7.1: gcc version 2.96 20000731 (Red Hat Linux 7.1 2.96-81)
RedHat 7.2: gcc version 2.96 20000731 (Red Hat Linux 7.1 2.96-98)
RedHat 7.3: gcc version 2.96 20000731 (Red Hat Linux 7.3 2.96-112)

Sample code:

#include <stdio.h>

static int constructor_called = 0;

static void __attribute((constructor)) call_before_main(void)
  constructor_called = 1;

int main(int, char **)
  printf("constructor called = %d\n", constructor_called);
  return 0;
Comment 1 Alan Cox 2002-12-15 17:36:11 EST
Verified still the case in 8.0
Comment 2 Jakub Jelinek 2004-10-02 16:53:05 EDT
When compiled with gcc-c++-3.4.2-2 or 3.3.4, it prints
constructor called = 1

Note You need to log in before you can comment on or make changes to this bug.