Bug 71490 - __attribute__ ((constructor)) doesn't work with g++
Summary: __attribute__ ((constructor)) doesn't work with g++
Alias: None
Product: Red Hat Linux
Classification: Retired
Component: gcc (Show other bugs)
(Show other bugs)
Version: 8.0
Hardware: i386 Linux
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact: Brian Brock
Depends On:
TreeView+ depends on / blocked
Reported: 2002-08-14 10:48 UTC by Wayne Price
Modified: 2007-04-18 16:45 UTC (History)
0 users

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

Attachments (Terms of Use)

Description Wayne Price 2002-08-14 10:48:54 UTC
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 22:36:11 UTC
Verified still the case in 8.0

Comment 2 Jakub Jelinek 2004-10-02 20:53:05 UTC
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.