Pierwsze tradycyjne "Hello world!"
From MorphOS Library
Grzegorz Kraszewski
"Hello World!" z użyciem biblioteki standardowej C
Używając biblioteki standardowej C, przykładowy program "Hello World!" możemy po prostu przepisać z podręcznika. Oto kod, dla przypomnienia:
#include <stdio.h> int main(void) { printf("Hello World!\n"); return 0; }
Kod źródłowy można skopiować do edytora tekstowego i zapisać jako helloworld.c. Aby go skompilować, otwieramy okno konsoli (z menu Ambienta, albo używając skrótu klawiszowego rcommand + n) i zmieniamy katalog bieżący na ten zawierający kod źródłowy. Uruchamiamy kompilator:
gcc -o helloworld helloworld.c
Kompilator stworzy plik wykonywalny helloworld, na moim systemie posiada on długość 10 340 bajtów. Warto zauważyć, że w MorphOS-ie rozszerzenia pilków nie są istotne więc dodanie .exe na końcu nazwy nie jest konieczne, chociaż możliwe. Tradycyjnie pliki wykonywalne w MorphOS-ie nie mają żadnego rozszerzenia nazwy. Opcja kompilatora −o specyfikuje nazwę tworzonego pliku wykonywalnego. Jeżeli opcja ta nie zostanie użyta, plik wynikowy zostanie nazwany a.out (z powodów historycznych).
Jak to opisano w rozdziale o SDK, biblioteka standardowa C jest dostarczana przez ixemul.library. Można to łatwo sprawdzić, śledząc aktywność dyskową programu za pomocą narzędzia Snoopium:
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:
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 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.
"Hello World!" With the MorphOS Native API
The 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 the 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 the native API are not portable (except for porting to AmigaOS and AROS to some degree).
- Native printf()-like functions do not support floating point numbers.
The "Hello World!" example using the native API is 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 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:
gcc -noixemul -o helloworld helloworld.c
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.
Note: excluding libnix is usually done for MorphOS components other than applications, like shared libraries or Reggae and MUI public classes. It can also be done for ordinary programs just to make them shorter, especially if a program is small. For bigger projects bytes saved by writing custom startup code are not usually worth the effort.