Coming soon - Get a detailed view of why an account is flagged as spam!
view details
1
Having trouble with file-level constructors not initializing correctly
Post Flair (click to view more posts with a particular flair)
Post Body

I'm having a problem with a project that was originally a C project and is being extended with C . E.g. all the APIs are C, and the application layer is C . It mostly works, except I seem to be unable to define constructors that do any sort of processing in pre-main. It's like the memory exists, and the reference is valid, but the constructor itself just doesn't execute. Running -Wall and -Wextra, the only error I get is that I'm missing a memory region definition for .ctors.

I did notice that the linker flags include -nostartfiles, so I tried disabling that flag but I ended up with some additional memory region warnings about the size of .ctors and .dtors changing and an undefined reference to main, which I thought was... odd.

So, any idea where I would start investigating the bug? I'm thinking maybe GCC is putting constructors in __main but the linker script is pointing the start of the program to something other than main()?

SSCCE below:

#include <iostream>
using namespace std;

class Foo {
    public:
        Foo() {
            bar_ = 10;
        };

        Foo(int bar) : bar_(bar) {};

        void setBar(int val) {
            bar_ = val;
        }

        int getBar() {
            return bar_;
        }

    private:
        int bar_;
};


Foo myObj;
Foo otherObj(10);

int main(){
    // Default Constructor
    cout << myObj.getBar() << endl;     // Prints 0
    myObj.setBar(10);
    cout << myObj.getBar() << endl;     // Prints 10

    // Parameterized constructor
    cout << otherObj.getBar() << endl;  // Prints 0
    otherObj.setBar(10);
    cout << otherObj.getBar() << endl;  // Prints 10

    // Parameterized constructor (local)
    Foo thirdObj(10);
    cout << thirdObj.getBar() << endl;  // Prints 10
}

Author
Account Strength
100%
Account Age
13 years
Verified Email
Yes
Verified Flair
No
Total Karma
130,597
Link Karma
12,951
Comment Karma
117,424
Profile updated: 13 hours ago
Posts updated: 9 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
6 years ago