grilotc
Posts: 15
Joined: Fri Aug 01, 2014 10:05 pm

C++ bare metal coding

Sun Jun 28, 2015 3:05 pm

From what I could see in the forum and elsewhere online most of the bare metal coding for the RPi is done in C.
Is there any particular reason for not using C++?

I mean, I know C does everything you need, but sometimes the conveniences that come with C++ (like scope
resolution) can make the code cleaner and easier to maintain.

JacobL
Posts: 76
Joined: Sun Apr 15, 2012 2:23 pm

Re: C++ bare metal coding

Mon Jul 06, 2015 1:12 pm

C++ requires a great deal more setup, more linker sections with specific requirements and a large runtime library, that you need to implement or integrate in a baremetal environment. My guess is that most people here just don't want to bother with all this for advantages that are pretty small at this level. Others might just feel like Linus: http://article.gmane.org/gmane.comp.ver ... .git/57918

dwelch67
Posts: 954
Joined: Sat May 26, 2012 5:32 pm

Re: C++ bare metal coding

Mon Jul 06, 2015 8:57 pm

C++ and other alternatives to C for bare metal have various amounts of overhead. C has very little and that very little is common with the others they just have more overhead. There are some cross compilers that have attempted to support C++ and have had some success. But the number and or availability of toolchains and users is far less than the just C folks.

C++ and other languages may have their benefits but at a cost, bare metal is where you get to see those costs, or at least someone does who does the work.

User avatar
jack.chaney
Posts: 15
Joined: Sun May 31, 2015 5:17 pm

Re: C++ bare metal coding

Thu Jul 23, 2015 12:55 pm

Hi, nice comments.
C++ does buy a substantial improvement in maintainability, and organization, but it does require a bit of overhead to make all of those features work properly. The mention of linkages and actual creation of the code are managed pretty well with GCC or whatever compiler is being used, and the issues of inefficiencies no longer should be a concern with the newer optimizers (just set the configurations correctly). Linkages are very often resolved with a startup section that does the initVars, initZvars, and callConstructors operations to instantiate global objects.
The major issue I have in using C++ for bare metal coding is many of the features are O/S based. The functions new and delete require malloc and free to be implemented, and stream requires stdin and stdout to be active. All need to have the back end operations created, and often a file manager of some sort. Unless you aren't doing dynamic class instantiation, or performing data transfer.
Take a look at your "C++" code and determine what features of C++ are being used, to see what can be eliminated or make a similar operation in regular C.

pgix
Posts: 34
Joined: Wed Jan 25, 2012 3:53 pm
Contact: Website

Re: C++ bare metal coding

Tue Aug 11, 2015 2:02 pm

Hi,

I have basic hobby kernels for IA32 and AMD64 which are written in C++ with assembler used where sensible / required. Take a look at the wiki page http://wiki.osdev.org/C%2B%2B, which has lots of information about supporting the C++ runtime features (and links to creating global constructors).

Essentially, if you consider C++ as "C with classes", there's very little work to do over what you would need anyway for a C kernel.
jack.chaney wrote:The functions new and delete require malloc and free to be implemented
If you write a kernel in C, you're going to be needing a malloc(n) (or kmalloc(n)) function anyway. In C++, adding the code:

Code: Select all

void* operator new  ( size_t count )
{
  return malloc(count); 
 // in my kernel, the heap is managed by a class and I call HeapManager::Allocate(count);
}
basically gets you a working 'new' operator. The same applies to delete / free and the array versions of these functions. You obviously have to make sure that new is not called before your heap manager is online, but this also applies to C and malloc.

The other big C++ feature that you will want is global and static classes. These can be added by calling GCC's own init functions (provided by the freestanding cross compiler). If you want RTTI or exceptions (should you use these in a kernel?), then things get a little more complex, but still not too bad. TBH, I've coped very nicely without either of these features anyway.
jack.chaney wrote:and stream requires stdin and stdout to be active
Yes, but this also applies to C. I have a "DebugConsole" class. For each architecture, a default debug device is added and you can very quickly have something like:

Code: Select all

debugConsole << "This is a debug message. Installed Memory: " << memCount << " bytes.";
Again, you would need some sort of debug output in C anyway, and writing C++-style versions is simply a case of wrapping. Later, when your device manager and VFS are online, you can redirect the early "DebugConsole" to stderr, or wherever you want to point it. In my core kernel, I make strong use of the singleton and factory patterns to aid all of this.

If you are more familiar with C++ in application development, I would strongly encourage anyone to give it a try in system programming land. Not for everyone, but I found it pretty effective.

Cheers,
Adam

shurikr
Posts: 2
Joined: Wed Dec 04, 2013 10:22 pm

Re: C++ bare metal coding

Tue Sep 01, 2015 6:22 am

The discussion of C++ vs C in bare metal constantly pops up once in a while. In my opinion, bare-metal developers are much closer to being "Electronics" or "Firmware" engineers that to being "Software" engineers. The developers with electronics-oriented mindset dislike using C++ due to its complexity and steep learning curve, using "C" is a natural choice for them. The developers with more software-oriented mindset (myself included), actually prefer using C++ instead of C even in bare-metal development, but they tend to dislike dealing with hardware and naturally choose to participate in development of Linux-based embedded systems or other non bare-metal development.

Using C++ in bare-metal environment is possible, but you need to get a certain level of proficiency in C++ in order to understand what you are doing and how to use it effectively.

I would recommend reading "Practical Guide to Bare Metal C++" (https://www.gitbook.com/book/arobenko/b ... pp/details) for those who want to master C++ in bare-metal environment.

Return to “Bare metal, Assembly language”