Trouble porting C++ code, MemoryPools

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome.
Tinkerer
Posts: 12
Joined: Wed Apr 19, 2017 2:47 pm
Has thanked: 2 times

Trouble porting C++ code, MemoryPools

Postby Tinkerer » Fri Jan 19, 2018 2:55 pm

Dear reader,

I'm trying to port an application that was originally written for mbed (https://os.mbed.com/teams/Xsens/code/MTi-1_example/file/cb7fbf7821b5/main.cpp/). This application uses MemoryPools and Queues, as far as I understand from the documentation the queues of mbed resemble a lot the mailboxes of ChibiOS. In order to be able to re-use most of the original code, I tried to refactor the code to ChibiOS. One of the things that I did is this (create MemoryPool for one message):

Code: Select all

 MemoryPool g_messagePool((size_t)(sizeof(XbusMessage)), NULL);


However, I get compiler errors:

Code: Select all

arm-none-eabi-g++ -c -mcpu=cortex-m4 -O2 -ggdb -fomit-frame-pointer -falign-functions=16 -ffunction-sections -fdata-sections -fno-common -flto -fno-rtti -Wall -Wextra -Wundef -Wa,-alms=build/lst/xbus_thread.lst -DCORTEX_USE_FPU=FALSE  -DTHUMB_PRESENT -mno-thumb-interwork -DTHUMB_NO_INTERWORKING -MD -MP -MF .dep/xbus_thread.o.d -mthumb -DTHUMB -I. -I../../chibios176/os/license -I../../chibios176/os/common/startup/ARMCMx/compilers/GCC -I../../chibios176/os/common/startup/ARMCMx/devices/STM32F3xx -I../../chibios176/os/common/ext/CMSIS/include -I../../chibios176/os/common/ext/CMSIS/ST/STM32F3xx -I../../chibios176/os/rt/include -I../../chibios176/os/common/oslib/include -I../../chibios176/os/common/ports/ARMCMx -I../../chibios176/os/common/ports/ARMCMx/compilers/GCC -I../../chibios176/os/hal/osal/rt -I../../chibios176/os/hal/include -I../../chibios176/os/hal/ports/common/ARMCMx -I../../chibios176/os/hal/ports/STM32/STM32F3xx -I../../chibios176/os/hal/ports/STM32/LLD/ADCv3 -I../../chibios176/os/hal/ports/STM32/LLD/CANv1 -I../../chibios176/os/hal/ports/STM32/LLD/DACv1 -I../../chibios176/os/hal/ports/STM32/LLD/DMAv1 -I../../chibios176/os/hal/ports/STM32/LLD/EXTIv1 -I../../chibios176/os/hal/ports/STM32/LLD/GPIOv2 -I../../chibios176/os/hal/ports/STM32/LLD/I2Cv2 -I../../chibios176/os/hal/ports/STM32/LLD/RTCv2 -I../../chibios176/os/hal/ports/STM32/LLD/SPIv2 -I../../chibios176/os/hal/ports/STM32/LLD/TIMv1 -I../../chibios176/os/hal/ports/STM32/LLD/USARTv2 -I../../chibios176/os/hal/ports/STM32/LLD/USBv1 -I../../chibios176/os/hal/ports/STM32/LLD/xWDGv1 -I./boards/SensorSlave -I../../chibios176/test/lib -I../../chibios176/test/rt/source/test -I./Ecat/Inc -I./Xsens/Inc -I../../chibios176/os/various/cpp_wrappers -I../../chibios176/os/hal/lib/streams -I../../chibios176/os/various ./Xsens/Src/xbus_thread.cpp -o build/obj/xbus_thread.o
arm-none-eabi-g++ -c -mcpu=cortex-m4 -O2 -ggdb -fomit-frame-pointer -falign-functions=16 -ffunction-sections -fdata-sections -fno-common -flto -fno-rtti -Wall -Wextra -Wundef -Wa,-alms=build/lst/main.lst -DCORTEX_USE_FPU=FALSE  -DTHUMB_PRESENT -mno-thumb-interwork -DTHUMB_NO_INTERWORKING -MD -MP -MF .dep/main.o.d -mthumb -DTHUMB -I. -I../../chibios176/os/license -I../../chibios176/os/common/startup/ARMCMx/compilers/GCC -I../../chibios176/os/common/startup/ARMCMx/devices/STM32F3xx -I../../chibios176/os/common/ext/CMSIS/include -I../../chibios176/os/common/ext/CMSIS/ST/STM32F3xx -I../../chibios176/os/rt/include -I../../chibios176/os/common/oslib/include -I../../chibios176/os/common/ports/ARMCMx -I../../chibios176/os/common/ports/ARMCMx/compilers/GCC -I../../chibios176/os/hal/osal/rt -I../../chibios176/os/hal/include -I../../chibios176/os/hal/ports/common/ARMCMx -I../../chibios176/os/hal/ports/STM32/STM32F3xx -I../../chibios176/os/hal/ports/STM32/LLD/ADCv3 -I../../chibios176/os/hal/ports/STM32/LLD/CANv1 -I../../chibios176/os/hal/ports/STM32/LLD/DACv1 -I../../chibios176/os/hal/ports/STM32/LLD/DMAv1 -I../../chibios176/os/hal/ports/STM32/LLD/EXTIv1 -I../../chibios176/os/hal/ports/STM32/LLD/GPIOv2 -I../../chibios176/os/hal/ports/STM32/LLD/I2Cv2 -I../../chibios176/os/hal/ports/STM32/LLD/RTCv2 -I../../chibios176/os/hal/ports/STM32/LLD/SPIv2 -I../../chibios176/os/hal/ports/STM32/LLD/TIMv1 -I../../chibios176/os/hal/ports/STM32/LLD/USARTv2 -I../../chibios176/os/hal/ports/STM32/LLD/USBv1 -I../../chibios176/os/hal/ports/STM32/LLD/xWDGv1 -I./boards/SensorSlave -I../../chibios176/test/lib -I../../chibios176/test/rt/source/test -I./Ecat/Inc -I./Xsens/Inc -I../../chibios176/os/various/cpp_wrappers -I../../chibios176/os/hal/lib/streams -I../../chibios176/os/various main.cpp -o build/obj/main.o
In file included from main.cpp:21:0:
./Xsens/Inc/xbus_thread.hpp:59:27: error: expected identifier before '(' token
  MemoryPool g_messagePool((size_t)(sizeof(XbusMessage)), NULL);
                           ^
./Xsens/Inc/xbus_thread.hpp:59:35: error: expected ',' or '...' before '(' token
  MemoryPool g_messagePool((size_t)(sizeof(XbusMessage)), NULL);
                                     ^
  .....
 


I don't understand what I'm doing wrong. The source is compiled using the C++ compiler, Eclipse can find the constructor,... What am I doing wrong?

Any suggestions? I could use the standard c interface, but I don't like giving up because of a non-understood compiler error.

edit:
I also get the error:

Code: Select all

'((XbusThread*)this)->XbusThread::g_messageDataPool' does not have class type   xbus_thread.hpp   /RT-STM32F303R8-SensorSlave/Xsens/Inc   line 80   C/C++ Problem

Apparently the g_messageDataPool is not seen as a class, but as another type, I guess it is compiled as a `MemoryPool` struct, from the C library (http://chibios.sourceforge.net/html/struct_memory_pool.html). How do I tell the compiler that I want to call the constructor from the C++ library?

Tinkerer
Posts: 12
Joined: Wed Apr 19, 2017 2:47 pm
Has thanked: 2 times

Re: Trouble porting C++ code, MemoryPools

Postby Tinkerer » Wed Jan 24, 2018 10:20 am

OK, this was completely user error.

I tried to instantiate a new MemoryPool with initializing parameters in the public: section of the class. This is not how it's supposed to be, I needed to make a default constructor, and within that constructor the initialization of the MemoryPools.

If anyone else tries this and gets the same errors: https://stackoverflow.com/questions/849812/c-construction-of-an-object-inside-a-class <- this is where you can find an explanation how to do it correctly.

User avatar
Giovanni
Site Admin
Posts: 10815
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 317 times
Been thanked: 267 times
Contact:

Re: Trouble porting C++ code, MemoryPools

Postby Giovanni » Wed Jan 24, 2018 10:32 am

Thanks for explaining, I expected people more inside C++ to answer.

Giovanni


Return to “Development and Feedback”

Who is online

Users browsing this forum: No registered users and 4 guests