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 completeness:
This source may be written in a text editor and saved as helloworld.c (copying it directly won't work, as nonbreaking spaces have been used for indentation on the webpage). To compile it, one opens a shell window (from the Ambient menu, or using the rcommand + n key combo) and changes current directory to the one, where the C source is located. The compiler is run as follows:
The compiler produces a helloworld executable, which is 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, it can be done. Traditionally MorphOS executables are named without any extensions. The −o compiler option specifies a 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 provided by ixemul.library. It can be easily confirmed by tracing the disk activity of helloworld using the Snoopium tool.
It can also be 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 the libnix alternative is recommended for use of the standard library. To use it, a −noixemul option has to be added, so the compiler is called as follows:
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 the executable. Every C handbook states, that printf() is the most expensive function of standard I/O, which has just been proven experimentally... On the other hand program activity, as traced with Snoopium, is reduced to three entries. No external resources are opened.
The MorphOS API (Application Programmer Interface) provides complete file and console input/output. In fact, functions in C and C++ standard libraries are wrappers around MorphOS native calls. Using the native API has the following advantages:
These advantages come at a price:
The "Hello World!" example using the native API is as follows:
The included header includes all things needed to use the dos.library, where the Printf() function is located. The function itself works the same as the standard library printf(), with some minor differences. The code is compiled with this command:
The command is the same as that used for the program using libnix and the standard library printf(), however, the standard C function is not used, so it is not linked. Now the executable size reduces to 13 500 bytes.
Why is libnix still needed in spite of the standard library calls not being used? Can't one just compile with −nostdlib? Other than the standard C library, libnix also provides application startup code. A program without this startup code can still work when launched from the shell, but will crash when started from Ambient. The startup code also provides an automatic MorphOS library opening and closing feature. So, excluding libnix completely is possible, but requires writing your own startup code and handling library opening and closing manually.