RIOT icon indicating copy to clipboard operation
RIOT copied to clipboard

c++: extern "C" in RIOT headers

Open dangnhat opened this issue 12 years ago • 11 comments

I wonder why RIOT doesn't have this definition:

#ifdef __cplusplus
extern "C" {
#endif 
...etc...
#ifdef __cplusplus
}
#endif 

in its header files? Because some functions like kernel_init will be called from assembly code (startup.s of cortex-m3 cpu, etc. ). I got some errors when try to compile my app (written in c++) with iot-lab_m3 platform using g++, and I think it's maybe because of name mangling of g++.

cheers.

dangnhat avatar Mar 29 '14 17:03 dangnhat

The problem is that we didn't try the C++ support to extensively, yet. That's why I'm happy to see you actually trying it. :)

Feel free to open pull-requests even if it only hot-fixes a problem for your own board. Otherwise, we'll look into the problem, of course.

Thanks for sharing your findings with us!

Kijewski avatar Mar 29 '14 18:03 Kijewski

Actually I can build both c, and c++ for my own board (using on iot-lab_M3 port) but in a different way and with some little tricks.

I use arm-none-eabi-gcc to compile both c and c++ file (with some flags to reduce the overhead of c++ for embedded devs), arm-none-eabi-gcc will indirectly invoke arm-none-eabi-g++ to compile c++ files. Then, link with arm-none-eabi-gcc and flag -lstdc++ (or using arm-none-eabi-g++). Remove _kill_r, and implement _kill (minimal) in syscall.c. Remove comment for __libc_init_array in startup file (because it will invoke all C++ static constructors).

It worked, but there is a mess of warnings because I compile with same flags for both c, and c++. Now I'm trying compiling RIOT with 2 compilers, gcc for c file, g++ for c++ and link with g++. But I think I get lost with the makefile of RIOT. (I'm not so good at makefile anyways.) So any hint is recommended :D .

I'm glad to make a PR when I get rid of these warnings.

cheers.

dangnhat avatar Mar 29 '14 18:03 dangnhat

If you "just" want to make a c++ application running on RIOT you can try PR #412 with PR #770, which compiles RIOT with gcc and the user application using g++. After compiling all objects are linked with g++ this way.
Have a look in the Makefile of PR #770 to see how to parametrize the c/c++ mixedmode.

BytesGalore avatar Mar 29 '14 19:03 BytesGalore

Thank BytesGalore. I have tried your way and it worked fine with iot-lab_M3 platform (with some additional compiler flags for stm32) :-D If I was right, CPPMIX var in the makefile of user's project will be used to explicitly to place this project in mixed mode and user's defined c++ compiler, flags will be used to compile user's app and link with RIOT source code which always be compiled with CC. Thus, I think it may have some problems:

If you "just" want to make a c++ application

  • Yeah, if my application have both c and c++ files it may cause some troubles.
  • c++ compiler and flags are defined in user's project makefile and they must be defined for every boards. So I think this will make user confuse when they need to maintain both c/c++ compiler, flags in the makefile in user's project and in board's folder. (e.g when they want to change compiler for their project, etc.)
  • Beside, user's makefiles are different from project to project. It can make some confusions.

In my opinion, we can provide both c and c++ compilers, flags at board-level (in the makefile.include of a board). And we can modify the RIOT/makefile.include to invoke c++ complier when there is any c++ files in user's project to compile these c++ files and place project in mixed mode (to use c++ compiler as linker). c file in user's project and in RIOT source will always be compiled by c compiler. Thus, the user's makefile for both c and c++ application can always be the same. But for now it just a idea and I haven't find any ways to do so. :)

Hope it will be helpful. :) cheers.

dangnhat avatar Mar 30 '14 19:03 dangnhat

Hi, thanks for your findings. I'm glad you found the PRs useful.

If I was right, CPPMIX var in ...

Yes, this is meant as a switch.

Yeah, if my application have both c and c++ files it may cause some troubles.

You're absolutely right, a c/c++ mixed mode for a user application currently requires to compile all user source files manually and finally link them against the RIOT object files. I will try to make some adjustments to the Makefile.include of #412 to automatically compile all provided source files in c++ mixedmode.

In my opinion, we can provide both c and c++ compilers, flags at board-level ...

We could use the CPPMIX switch to enable the specific c++ board flags if needed. I use the MSB-A2 board which does not require to adjust flags for c++ mixedmode, so we would have to find out which boards are affected.

BytesGalore avatar Mar 31 '14 06:03 BytesGalore

Hi, For cortex-m mcu (iot-lab_M3, stm32f4disco) we can use -fno-exceptions and -fno-rtti when compiling c++ files, it can reduce a lot of KBytes of image (maybe ~ 50KB). I'm not familiar with ARM7 mcus so I haven't tried with msba2 platform yet. u could try to use these flags to see if it can help to reduce something. :)

dangnhat avatar Apr 02 '14 16:04 dangnhat

Do you maybe know if it is possible to use this on a per file / per module basis? I wouldn't like to disable RTTI and exceptions for applications. Maybe a user needs it.

Kijewski avatar Apr 02 '14 16:04 Kijewski

Do you maybe know if it is possible to use this on a per file / per module basis?

Sadly, I don't have any experience about this. :-P

dangnhat avatar Apr 02 '14 16:04 dangnhat

One could use the compiler flags on a per file basis and compile shared objects. But the consequence would be that if one object uses a function from another shared object that throws an exception, it would most probably crash mercilessly. So it can be done, but with care.

BytesGalore avatar Apr 08 '14 10:04 BytesGalore

addressed by #1788, #1793, #1803, #1806 and #1807.

BytesGalore avatar Oct 14 '14 09:10 BytesGalore

Great achievement!

OlegHahm avatar Oct 14 '14 09:10 OlegHahm