C++ Bare Bones: Difference between revisions

[unchecked revision][unchecked revision]
Content deleted Content added
Kevin (talk | contribs)
m Interwiki
Solar (talk | contribs)
Cleaned up some inconsistency on main() / kmain() / Entry.cpp / kernel.cpp. Removed C/C++ loader code - with no stack set up, calling C code isn't that smart. See discussion.
Line 81:
 
==Calling Constructors & Destructors==
The constructors have to be executed as quickly as possible after booting your kernel (just make sure you execute them before you actually use any of the objects that require them). Destructors should preferably be called somewhere before your kernel shuts the computer down or simply returns from the mainkmain function.
 
Below you'll find a couple of examples on how to do this.
Line 106:
jb .body
 
call main kmain ; call kernel proper
 
static_dtors_loop:
Line 142:
jb 1b
 
call main kmain # call kernel proper
 
# calling static destructors
Line 159:
</source>
 
===C/C++=kmain.cpp==
<source lang="cpp">
extern start_ctors, end_ctors, start_dtors, end_dtors;
 
void loader(void)
{
//- call all the static constructors in the list.
for(unsigned long *constructor(&start_ctors); constructor < &end_ctors; ++constructor)
((void (*) (void)) (*constructor)) ();
 
//- call kernel proper
main();
 
//- call all the static destructors in the list.
for(unsigned long *destructor(&start_dtors); destructor < &end_dtors; ++destructor)
((void (*) (void)) (*destructor)) ();
}
</source>
 
==Entry.cpp==
Now, all that is needed is to declare C style linkage for the kernel entry function, so that its name will not get mangled to C++ linkage style and you can call it from your multiboot header Assembly file:
 
<source lang="cpp">
extern "C" void mainkmain(struct mb_header *header, unsigned magic)
{
// write your kernel here
Line 195 ⟶ 177:
</pre>
 
'''loader''' should in its turn call "mainkmain" (or something like "_main" if your linker requires you to add a leading underscore).
 
==Compiler Options==
Line 201 ⟶ 183:
 
<source lang="bash">
g++ -o mainkmain.o -c kernelkmain.cpp -Wall -Wextra -Werror -nostdlib -fno-builtin -nostartfiles -nodefaultlibs -fno-exceptions -fno-rtti -fno-stack-protector
</source>