Library Calls: Difference between revisions

no edit summary
[unchecked revision][unchecked revision]
(Reformat, mark as in progress because this thing needs my love)
No edit summary
 
(4 intermediate revisions by 3 users not shown)
Line 1:
{{In_Progress}}
{{FirstPerson}}
{{You}}
 
<sourcesyntaxhighlight lang="c">
#include <stdio.h>
int answer = 42;
printf("The answer is %d!\n", answer);
</syntaxhighlight>
</source>
 
We all know this construct from our userspace programming experiences. We never really thought about how it worked, at least for some time. You ''#include'', you get to use "the system".
Line 19 ⟶ 21:
Let us have a look at the above code fragment, again. You could just as well write:
 
<sourcesyntaxhighlight lang="c">
int printf(const char * format, ...);
char * msg = "World";
printf("Hello %s!\n", msg);
</syntaxhighlight>
</source>
 
The header ''<stdio.h>'', in this case, does serve no other purpose than to tell the compiler that there is a function ''printf()'' that takes a parameter list consisting of at least a ''const char'' pointer, and returns ''int''. Nothing else is necessary, and mere convenience so you don't have to write your own declarations or ''#include'' each function individually.
Line 73 ⟶ 75:
So you provide a lightweight <tt>printf()</tt> for kernel space. You don't even have to bother to call it <tt>kprintf()</tt> or something as your kernel binary will never be '''linked''' with userspace code. Using some preprocessor ''#ifdef'' magic, you can even use the very same ''<stdio.h>'' header file as for userspace code, reducing redundancy and potential error sources: The preprocessor symbol <tt>__STDC_HOSTED__</tt> (with two leading and trailing underscores that the Wiki stubbornly interprets as boldface markup) is undefined when you set ''-ffreestanding''...
 
<sourcesyntaxhighlight lang="c">
#ifdef __STDC_HOSTED__
// Userspace declarations
Line 79 ⟶ 81:
// Kernelspace declarations
#endif
</syntaxhighlight>
</source>
 
[[Category:Standard Libraries]]
[[Category:FAQ]]