Difference between revisions of "The First Traditional "Hello world!""
From MorphOS Library
m (→"Hello World!" With the Standard C Library) |
(→"Hello World!" With MorphOS Native API: New contents.) |
||
Line 32: | Line 32: | ||
=="Hello World!" With MorphOS Native API== | =="Hello World!" With MorphOS Native API== | ||
+ | |||
+ | MorphOS API (''Application Programmer Interface'') provides complete file and console input/output. In fact, functions in C and C++ standard libraries are more or less complex wrappers around MorphOS native calls. Using the native API has following advantages: | ||
+ | |||
+ | * Programs are much shorter. | ||
+ | * Programs are faster, thanks to stripping some layers of abstraction. | ||
+ | * Programs are less resource hungry. | ||
+ | * Native API gives full access to MorphOS specific features. | ||
+ | |||
+ | These advantages come at a price: | ||
+ | |||
+ | * Programs using native API are not portable (except of porting to AmigaOS and AROS to some degree). | ||
+ | * Native ''printf()''-like functions do not support floating point numbers. | ||
+ | |||
+ | |||
+ | The "Hello World!" example using native API looks as follows: | ||
+ | |||
+ | |||
+ | <tt>#include <proto/dos.h><br><br> | ||
+ | int main(void)<br> | ||
+ | {<br> | ||
+ | Printf("Hello World!\n");<br> | ||
+ | return 0;<br> | ||
+ | }</tt> | ||
+ | |||
+ | |||
+ | The included header includes all things needed to use the ''dos.library'', where ''Printf()'' function is located. The function itself works the same as standard ''printf()'', with some minor differencies. |
Revision as of 11:49, 23 October 2010
Grzegorz Kraszewski
"Hello World!" With the Standard C Library
With the standard C library, one can use the "Hello World!" example exactly as found in a C handbook. It is given below, just for completness:
#include <stdio.h>
int main(void)
{
printf("Hello World!\n");
return 0;
}
This source may be copied to a text editor and saved as helloworld.c. To compile it, one opens a shell window (from Ambient menu, or using rcommand + n key combo) and changes current directory to the one, where C source is located. The compiler is ran as follows:
gcc -o helloworld helloworld.c
The compiler produces helloworld executable, which has 10 340 bytes on my system. Note that MorphOS pays little attention to filename extensions, so ending the executable name with .exe is not needed, however possible. Traditionally MorphOS executables are named without any extensions. The −o compiler option specifies desired executable name. If this option is not given, the executable will be named a.out (for some historical reasons).
As stated in the SDK section, the standard C library will be delivered by ixemul.library. It can be easily confirmed by tracing disk activity of helloworld using Snoopium tool.
It can be also seen that many other libraries are also opened including ones related to TCP/IP networking. It looks like overkill for such a small program. This happens, because ixemul.library creates a complete unixlike environment for the application, which is not needed in this simple case. That is why libnix alternative is recommended for the standard library. To use it, a −noixemul option has to be added, so the compiler is called as follows:
gcc -noixemul -o helloworld helloworld.c
The generated executable is much larger (30 964 bytes here), which just confirms the fact, that libnix, which is now in use, is a statically linked library. Size of functions used adds to the size of executable. Every C handbook states, that printf() is the most expensive function of standard I/O, which has been just proven experimentally... On the other hand program activity, as traced with Snoopium, reduces to three entries. No external resources are opened.
"Hello World!" With MorphOS Native API
MorphOS API (Application Programmer Interface) provides complete file and console input/output. In fact, functions in C and C++ standard libraries are more or less complex wrappers around MorphOS native calls. Using the native API has following advantages:
- Programs are much shorter.
- Programs are faster, thanks to stripping some layers of abstraction.
- Programs are less resource hungry.
- Native API gives full access to MorphOS specific features.
These advantages come at a price:
- Programs using native API are not portable (except of porting to AmigaOS and AROS to some degree).
- Native printf()-like functions do not support floating point numbers.
The "Hello World!" example using native API looks as follows:
#include <proto/dos.h>
int main(void)
{
Printf("Hello World!\n");
return 0;
}
The included header includes all things needed to use the dos.library, where Printf() function is located. The function itself works the same as standard printf(), with some minor differencies.