2

I am building an Xcode static library project, where I have a class defined as:

class New
{
    public:
    New() {
        // Do something
    };
    static New fNew;
};

__attribute__((used)) New New::fNew;

The static global fNew should not be dead-code stripped by the linker as it has been force declared as ((used)). But I cannot see the constructor getting called for New. Any idea why this is happening?

EDIT: Expectedly, when I add a dummy function to return a pointer to fNew

void* getDummyReference()
{
    return (void*)&New::fNew;
}

and call the function from another file in the same static library project, I do not observe the issue any longer and the constructor is called as expected.

Why would the static global be stripped even with atribute((used))?

AbhinavChoudhury
  • 1,167
  • 1
  • 18
  • 38
  • `fUsed` or `fNew`? Also, how do you check if the constructor is called? Since it's empty the compiler might have optimized the call away, have you tried building with no optimization at all? Have you checked the generated assembly code? – Some programmer dude May 13 '15 at 07:28
  • fNew. Also the constructor is not empty in the actual code, so I can actually put a breakpoint. The nm dump of the object file shows the global object fNew as present, yet the breakpoint doesn't hit in the constructor. – AbhinavChoudhury May 13 '15 at 11:36
  • *When* do you add the breakpoint? *How* do you set the breakpoint? Can you please try and create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) to show us, and also include your debugger session? And if you e.g. print something in the constructor it's not printed? Is the file linked into the program? – Some programmer dude May 13 '15 at 11:43

0 Answers0