Bug 71490

Summary: __attribute__ ((constructor)) doesn't work with g++
Product: [Retired] Red Hat Linux Reporter: Wayne Price <w.price>
Component: gccAssignee: Jakub Jelinek <jakub>
Status: CLOSED CURRENTRELEASE QA Contact: Brian Brock <bbrock>
Severity: medium Docs Contact:
Priority: medium    
Version: 8.0   
Target Milestone: ---   
Target Release: ---   
Hardware: i386   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2004-10-02 20:53:05 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

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:
Always

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 
behaviour.

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