C++: Difference between revisions

[unchecked revision][unchecked revision]
Content deleted Content added
m The C and C++ bare bones articles were merged for maintainability
Rework the article and add a new introduction
Line 4:
 
A lot of features C++ offers can be used on-the-fly; they require no additional support or code to use them properly (e.g. templates, classes, inheritance, virtual functions). There are however other parts of C++ that do require runtime support, which will be discussed in this article.
 
== Introduction ==
 
If you have created a C++ kernel as documented in the [[Bare Bones]] article, then many C++ features are already available and work out the box. However, your kernel does not yet satisfy the ABI and you cannot be confident that the compiler will not emit problematic code, even if you stick to the intersection of C and C++. In particular, you may need to initialize further CPU state to enable the floating-point registers and instructions, as the compiler has every reason to think floating point registers and instructions are available by default.
 
However, the compiler will assume that all the C++ runtime support is available by default, however you are not linking in libsupc++ into your C++ kernel, which implements the necessary run-time support. This is why you are passing -fno-rtti and -fno-exceptions to your cross-compiler to let these runtime features are unavailable. Going further, you should link in libsupc++ into your kernel, but at the moment it's known to not be readily accessible to those starting out with operating systems development and the GCC build process doesn't cross-compile it properly for the bare -elf platforms by default.
 
You also need to call the global constructors as documented in [[Calling Global Constructors]] to satisfy the ABI requirement that the program initialization tasks are properly called.
 
== Pure virtual functions ==
 
If you want to use pure virtual functions, your compiler needs onea single support function. It is only called in case a pure virtual function call cannot be made (e.g. if you have overridden the virtual function table of an object). But nonetheless your linker will complain about unresolved symbols, if you use pure virtual functions and don't provide this support routine. It is a C++ requirement to provide this back-up function.
 
Enabling pure virtual functions in GCC is fairly straightforward. All you need to do is add the function below to one of your C++ source files (and make sure it is linked in). It is not necessary to declare this function first, the definition alone is good enough for GCC. The function itself doesn't even need to do anything (and it doesn't in most implementations), it just needs to be "there" just in case.
 
Below you will find an example of an implementation in respectively GCC and Visual C++.
 
<source lang="cpp">
Line 18 ⟶ 27:
}
</source>
 
Or, if you happen to use Visual Studio:
 
<source lang="cpp">
Line 44 ⟶ 55:
 
=== GCC ===
 
Note: This appears to be specific to the Itanium platform. For IA-32/x86/i386 and amd64/x86_64, please check out [[Calling_Global_Constructors]] instead.
 
According to the [http://refspecs.freestandards.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/baselib---cxa-atexit.html Itanium C++ Application Binary Interface] (which '''g++''' follows and VC++ does not) the function '''__cxa_atexit''' is used to register a destructor that should be called when a shared library needs to be unloaded. It should insert a function pointer with maximum 1 accompanying argument and the handle of the object or shared resource to be destroyed into a table.
 
Line 63 ⟶ 77:
</source>
 
The construction of global/static objects is the same as of older versions of GCC. After you have called the objects constructor GCC automatically calls the function
==== Versions after GCC 3.2 ====
The construction of global/static objects is the same as of older versions of GCC. After you have called the objects constructor GCC automatically calls the function
 
<source lang="cpp">