📄 os-faq-cpp.html
字号:
<html><head> <title>Operating Systems FAQ :: C++ Programming</title> <link rel=stylesheet type="text/css" href="default.css"></head><body><TABLE border="0" width="100%"> <TR> <TD><H2><A name="start">Doing a kernel in C++</A></H2></TD> </TR> <TR> <TD>(This was taken from a message on the OS-Dev message board)<p>Using C++ is quite easy for the most part just a few snags that have to bedelt with namely the builtin functions that g++ inserts into your code usethe following make line to get rid of most of them.<pre>g++ -nostdlib -nostdinc -fno-builtin -fno-exceptions</pre><p>of course it is easier to just define those options as some macro the only builtin function you will have to deal with after this willbe __builtin-delete for deleting this __builtin_new for creating newobjs and __builtin_vec_new for arrays of objs simply. <p>Simply don't use NEW and you are safe from the last two builtins andjust declare this in a header file somewhere<pre>void inline __builtin_delete(void* arg){}; </pre><p>then things should compile without errors if you want to be able to use newand new[] then you have to define the builtins correctly just like regularfunctions where new and new[] take a size number and an unsigned int andreturn a void*<p>i use flat binary mode for the kernel initialization stuff. Compileeverything as modules using (nolib is the crap i described earlier) <pre>g++ $(NOLIB) file.cpp -r </pre>the link them in the end using (this is 1 line, but split over two for display)<pre>ld $(NOLIB) -o kernel.bin -e main -oformat binary-Ttext 0x00100000 file1.o file2.o file3.o ...</pre><p>The entry point where execution will start is int main(); i find that ifyou define main() after you define any other functiosn the linker doesn'tproduce code properly for binary output so just make main the first functionin your file and make sure that it is the first file in the list of fileslinked by ld in the line i described earlier.<p>Finally -Ttext 0x00100000 tells the linker where the file will be in memorywhen it gets executed is at 1MB mark. </TD> </TR></TABLE><P> </P><TABLE border="0" width="100%"> <TR> <TD><H2><A name="rtti">Aiyah! Whats RTTI? (Run Time Type Info)</a></H2></TD> </TR> <TR> <TD>(This info take from a message on the OS-Dev message board) <p>This is the way i understand RTTI...<br>RTTI is for Run Time Type Information. Because you are using a virtual functionand inherritance, it would be possible to pass two completely different objectsto the same function. Because the function is virtual the compiler cannot createtwo versions at link time and sort it out ahead of time, instead it has to befigured out just before it is executed.<p>RTTI is a means of (for lack of a better term) enumerating types while themachine runs and then providing a means to get that type information for anyobject (type number). I think you did the rignt thing by putting -no-rtti inthe compile string but if you ever dynamically change an object from one typeto another during run time you will get into trouble.<p>This eg should clarify for all what i think would happen (in a C equivalentmanner).<pre>print(char* string) { ...prints a string which is given as a 32bit pointer on the stack... } print(int number) { ...prints a 32bit integer that is passed as an argument on the stack } //now for the types int num1=12345; char name[]="ABCDEFG; int_or_string variabletype = "I am a string for now" //now call them print(num1); print(name); print(varialbetype); variabletype = 98765; print(variabletype); variabletype = "a string again"; print(variabletype); </pre><p>The compiler can figure out a compile time that print(num1); is a call to theinteger version of print() and print(name) is a call to the string version.<p>But because our variabletype can change from one type (class) to another it isnot possible at compile time to determine which function to call until we areactually calling it. </TD> </TR></TABLE><P> </P><TABLE border="0" width="100%"> <TR> <TD><H2><A name="disable_rtti">How do I disable RTTI in GCC?</A></H2></TD> </TR> <TR> <TD>You can disable RTTI in gcc by adding the switch "<b>-fno-rtti</b>" </TD> </TR></TABLE><P> </P><TABLE border="0" width="100%"> <TR> <TD><H2><A name="new_delete">Can I use NEW and DELETE in my kernel?</A></H2></TD> </TR> <TR><td>(This message taken from a message on the OS-Dev message board)<p>The new a delete will be available if you define them as functions and it ispossible to (from real 16bit mode setup a gdt and idt in a kind of crudefashion then turn on the pmode bit and long jump into the code using theright selector and offset which will start right into c++ code with objectsand everything you want. </TD> </TR></TABLE></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -