5

For a microcontroller project I'm working on, I'm writing a shared library. I'm mixing C and C++, mainly because of all the pointer arithmic that needs to be done in a low level part and because then my code easily maps on the C libraries. On a different microcontroller however, I want to use C++ for the hardware part. As a HAL I use a struct of function pointers to give the implementation.

Now, the pointer is declared in a common file, as:

extern HALPointer * _hal_ptr;

now, in my C file I simply say

HALPointer _hal_ptr = (HALPointer) {
   .init = &Native_init,
   .destroy = &Native_destroy,
   .write = &Native_write,
   ...
}

However, in my C++ file, I cannot used the named initialisation lists. I'm using G++ with support for c++98, gnu++98, c++0x and gnu++0x. Is there any way to do this?

Edit: This is done in the C++ file as, to use C calling convention etc.

extern "C" {

}

It is also not possible to use a function to do this. Also, the order of elements is probably going to change, hence the need for named member initialization.

Edit2: In C++ Structure Initialization, some solutions are presented, which unfortunately either do not work, or are not suitable. Since the order of elements is bound to change (and items may be added etc), non named versions are not an option. The last solution does not compile. The other solution is terrible to say the least.

Edit3: Sticked with the terrible solution:

extern "C" {
   HALPointer _hal_ptr = (_hal_tr = HALPointer(), 
                     _hal_ptr.init =      &PRU_init,
                     _hal_ptr.destroy =   &PRU_destroy,
                     ....
                     _hal_ptr);
}
Community
  • 1
  • 1
  • Can you initialize it from a function? – Pubby May 21 '13 at 13:09
  • 1
    Compound literals are not available in c++ and neither are designators. `_hal_ptr` would need to be an object (not a pointer) and then specify the initializers in the order of the member declarations. – hmjd May 21 '13 at 13:10
  • Curiosity, PRU_init is a c function name, or a c++ function name? Remember, c++ mangle function names. – Amadeus May 21 '13 at 13:36

0 Answers0