Are the C++ Wrappers Correct? Topic is solved

Report here problems in any of ChibiOS components. This forum is NOT for support.
spinchak
Posts: 2
Joined: Thu Apr 14, 2022 12:23 am

Are the C++ Wrappers Correct?  Topic is solved

Postby spinchak » Thu Apr 14, 2022 1:02 am

I tried to build the C++ demo project in demos/STM32/RT-STM32F407-DISCOVERY-G++ on ChibiOS 21.11.1 stable. Using the default USE_OPT in the top level Makefile everything builds seemingly correctly. However, one of the default opts is "-O2" If I use "-Og" or "-O0" to make debugging a bit more straightforward, I get linking errors.

Code: Select all

c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\ME\AppData\Local\Temp\ch.elf.GQwVbg.ltrans1.ltrans.o:(.rodata._ZTVN10chibios_rt10BaseThreadE+0x8): undefined reference to `__cxa_pure_virtual'
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\ME\AppData\Local\Temp\ch.elf.GQwVbg.ltrans1.ltrans.o:(.rodata._ZTVN10chibios_rt10BaseThreadE+0xc): undefined reference to `__cxa_pure_virtual'
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\ME\AppData\Local\Temp\ch.elf.GQwVbg.ltrans1.ltrans.o:(.rodata._ZTVN10chibios_rt16BaseStaticThreadILi128EEE+0x8): undefined reference to `__cxa_pure_virtual'
c:/chibistudio/tools/gnu tools arm embedded/10.3 2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\ME\AppData\Local\Temp\ch.elf.GQwVbg.ltrans1.ltrans.o:(.rodata._ZTVN10chibios_rt16BaseStaticThreadILi256EEE+0x8): undefined reference to `__cxa_pure_virtual'
collect2.exe: error: ld returned 1 exit status


I think that the "-O2" optimization is letting the compiler or linker recognize that there is no need for the __cxa_pure_virtual function and throw it out. Under the "-Og" or "-O0" optimization, the function symbols remain in the ch.elf translation unit, but it is not being actually included in syscalls.o.

In the current /os/various/cpp_wrappers/chcpp.mk file, syscalls.c is included in the ALLCSRC macro.

Code: Select all

CHCPPSRC   = $(CHIBIOS)/os/various/syscalls.c
...
ALLCSRC   += $(CHCPPSRC)


syscalls.c is compiled by $(CC) and not $(CPPC). With the default USE_OPT and USE_COPT, files that are compiled by $(CC) do not have __cplusplus set and so the __cxa_pure_virtual function is not included in the compiled syscall.o. The default rules.mk does not let you compile .c files with the c++ compiler.

What is the best solution for fixing this?
  • Make a separate syscalls.cpp containing the __cxa_pure_virtual function?
  • Fiddle with rules.mk to force $(CC) to call $(CPPC) or use __cplusplus macro?
  • Always compile with "-O2"?
  • Remove the #ifdef __cplusplus and include the __cxa_pure_virtual for everyone?

spinchak
Posts: 2
Joined: Thu Apr 14, 2022 12:23 am

Re: Are the C++ Wrappers Correct?

Postby spinchak » Thu Apr 14, 2022 1:31 am

I tried to implement possible solution 4, make __cxa_pure_virtual compile for everyone.

Code: Select all

--- syscalls.c.old      2022-04-13 19:09:15.866085000 -0500
+++ syscalls.c  2022-04-13 19:10:00.605724900 -0500
@@ -216,9 +216,11 @@

 #ifdef __cplusplus
 extern "C" {
+#endif
   void __cxa_pure_virtual(void) {
     osalSysHalt("Pure virtual function call.");
   }
+#ifdef __cplusplus
 }
 #endif


Unfortunately, this leads to a new issue. "osalSysHalt" is not included in the header files required by syscalls.c. Can we replace this function with a different halt function? Perhaps "chSysHalt"? After switching out the halt functons, the demo seems to build correctly with "-Og" or "-O0", but am I missing something here?

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

Re: Are the C++ Wrappers Correct?

Postby Giovanni » Thu Apr 14, 2022 12:28 pm

Hi,

I think calling chSysHalt() is the correct way in that module.

Moving this in "bug reports" for handling.

Giovanni

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

Re: Are the C++ Wrappers Correct?

Postby Giovanni » Sun Apr 24, 2022 7:37 am

Hi,

Fixed in repository, all branches.

Giovanni


Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 2 guests