0

I have a sample code as shown below:

 class myclass
{
public:
    static int j;
    myclass(){};
    ~myclass(){};
};

int main(int argc, char** argv) {
    myclass obj;
    return EXIT_SUCCESS;
}

Now I have declared a static int inside myclass and although I have not defined it, the compiler does not give me any errors until I began using the static variable. Why is that?

1
  • 1
    You only need a definition if you odr-use it. Commented Oct 7, 2014 at 17:03

2 Answers 2

2

Because these are linker errors, not compiler errors. Linker errors never arise until you use an undefined symbol.

Sign up to request clarification or add additional context in comments.

Comments

2

"the compiler does not give me any errors until I began using the static variable. Why is that?"

Because it didn't need to be linked with your code until that point (when you start to use it). Unused code is ignored / stripped off by the linker.

1 Comment

This issue has nothing to do with the linker discarding unused symbols' definitions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.