2

I am trying to port c++ application to arm board with gcc tools (using RTOS). But my static const constructors are not being called.

Simple code:

class TestClass {
public:
    TestClass();
    TestClass(int m);
    TestClass(const TestClass& other);
    ~TestClass();
    int getM() const;
    const TestClass& operator = (const TestClass& other);
private:
    int m;    
};

class TestInitClass {
    static const TestClass TestClassObj;
};

const TestClass TestInitClass::TestClassObj = TestClass(5);

I provide class definitions. But when I call this with TestInitClass::TestClassObj.getM() it returns me 0.

There are multiple problems:

  1. My static const is getting allocated in .bss section. It is not getting in .ctors sections (this may be linker script problem?!)
  2. And even if it gets in .ctors section, how do I call these constructors
  3. When I use static c++ library how should I call them?

Thanks

itdl
  • 221
  • 2
  • 4

2 Answers2

1

Most likely you forgot to either use collect2 or GNU linker. See:

  • Your second link indicates the latest version of gcc was released in 1994! It's really really old, I wonder if the arguments there are still valid. – Shahbaz Nov 16 '11 at 23:30
  • @Lzerenko thanks for the links. I use arm-none-eabi-g++ which calls collect2. So that is not a problem. – itdl Nov 17 '11 at 12:31
0

To use gcc and RTOS, you should have "ld" script, that describe where put what in memory, it may for example describe how to handle code of global constructors.

About calling of constructors. You can look at source code of eCos:
http://ecos.sourceware.org/
For arm architecture you can look at vectors.S and hal_misc.c at packages/hal/arm/arch/current/src directory. vector.S contains something like:

bl      cyg_hal_invoke_constructors

and in hal_mics.c implementation of this function.

fghj
  • 8,898
  • 4
  • 28
  • 56
  • Thanks for eCos. I am using Toppers/Asp. Currently kernel is C library. While linking it is not able to generate .init_array for global constructors. (It works well only problem is static/global constructors of C++). Also strange thing is entries in .init_array are not 4 bytes aligned. – itdl Nov 17 '11 at 12:38
  • ecos also just a library. That can manage your threads and implement C runtime and partly C++ runtime. – fghj Nov 17 '11 at 13:58