AnsweredAssumed Answered

C++ constructor of static objects not called

Question asked by Gustav Andersson on Feb 8, 2017
Latest reply on Feb 10, 2017 by Erwan Y

Hello,

I am trying to use C++ in the SPC560Bxx mcu using SPC5 Studio and the free gcc vle toolchain (4.9.2).

 

However, it looks like constructors aren't called as I expected.

In the following example, I expect to end up in the first case of the if statement, but I end up in the else case.

class Foo {
public:
  int val;
  Foo() {
    val = 42;
  }
  void do_something() {
    if (val == 42) {
      printf("I expect to end up here");
    } else {
      printf("But I end up here");
    }
  }
};
static Foo myObject; // this ought to call the Foo's constructor


int main(void) {
  ...
  myObject.do_something();
  ...
}

 

From what I understand this is because the crt0.s doesn't initialize the static objects on startup.

 

Does anyone have a solution to my problem or a working c++ example they can share?

Outcomes