Dieser Artikel ist Bestandteil der Serie Build Systeme, in welcher verschiedene Systeme zum Softwarebau gegenüber gestellt werden. Um zum übergeordneten Artikel zu gelangen, klicken Sie bitte hier.


Das Testprogramm

Alle Build Systeme sollen anhand desselben Beispiels untersucht werden. Es handelt sich um ein einfaches C-Programm, das neben der Standard C-Bibliothek eine externe Abhängigkeit zu libcaca0 beinhaltet. Somit wird verhindert, dass ein einfacher Aufruf von gcc ohne weitere Parameter genügt, um das Programm zu compilieren und zu linken.

libcaca ist eine einfache Ascii Art-Bibliothek mit wenig Systemanforderungen. Sie wurde gewählt, da sie relativ kompakt ist und eine einfache API besitzt, welche das Testprogramm nicht unnötig verkompliziert.

Testprogramm

Das Programm zeichnet zwei Boxen mit einem "Hello, World!"-Text und einem Hinweis, dass das Programm mit ESC beendet werden kann. Da hierfür eine einfache Polling-Schleife verwendet wird, ist das Warten sehr CPU-intensiv. Auf eine Verbesserung dessen via usleep() und dergleichen wird dennoch verzichtet.

In seiner Grundversion besteht das Programm nur aus einer Quelldatei:

Quelldatei single_file.c

/*
*  single_file.c
*  Copyright 2009 Dennis Schulmeister <dennis()ncc-1701a.homelinux.net>
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*/

/* libc standard */

#include <string.h>

/* Non-standard library */
#include
<caca0.h> /* libcaca-dev */

int main(int argc, char** argv) {
    
/* Set up a display to draw upon */
    if (caca_init()) {
        return 1;
    }

    
/* Draw something */
    {
        int width  = caca_get_width();
        int height = caca_get_height();

        
/* Background */
        caca_set_color(CACA_COLOR_GREEN, CACA_COLOR_GREEN);
        caca_clear();

        
/* First message */
        caca_set_color(CACA_COLOR_RED, CACA_COLOR_BLACK);
        caca_draw_thin_box(11, (width - 2), 3);

        int i;
        for (i = 1; i <= width - 2; i++) {
            caca_putstr(i, 2" ");
        }

        char *message = "Hello, World!";
        caca_putstr((width - strlen(message)) / 22, message);

        
/* Second message */
        caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLUE);
        caca_draw_thin_box(35, (width - 6), 3);

        for (i = 4; i <= width - 5; i++) {
            caca_putstr(i, 6" ");
        }

        char *message1 = "Press ESC to exit";
        caca_putstr((width - strlen(message1)) / 26, message1);

        
/* Show it */
        caca_set_window_title(message1);
        caca_refresh();
    }

    
/* Wait for ESC key */
    {
        int quit = 0;
        while (!quit) {
            int event;
            while ((event = caca_get_event(CACA_EVENT_KEY_PRESS))) {
                if ((event & 0xffff) == CACA_KEY_ESCAPE) {
                    quit = 1;
                }
            }
        }
    }

    
/* Clean up */
    caca_end();
    return 0;
}

Um das Beispiel jedoch realistischer zu gestalten, existiert noch eine Version, in welcher das Programm in mehrer Quelldateien zerlegt wurde. Und zwar wurden zwei Funktionen void msg1() und void msg2() geschaffen, welche die jeweilige Nachricht auf dem Bildschirm ausgeben. Jede Funktion befindet sich in einer eigenen Quelldatei (msg1.c und msg2.c) zu der es eine Headerdatei (msg1.h und msg2.h) gibt.

Hier die beiden Headerdateien:

Headerdatei msg1.h
/*
*  msg1.h
*  Copyright 2009 Dennis Schulmeister <dennis()ncc-1701a.homelinux.net>
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*/


#ifndef __MSG1
#define __MSG1

extern void msg1();

#endif
Headerdatei msg2.h
/*
*  msg2.h
*  Copyright 2009 Dennis Schulmeister <dennis()ncc-1701a.homelinux.net>
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*/

#ifndef __MSG1
#define __MSG1

extern char *message1;
extern void msg2();

#endif

Und hier die zugehörigen Quelldateien;

Quelldatei msg1.c
/*
*  msg1.c
*  Copyright 2009 Dennis Schulmeister <dennis()ncc-1701a.homelinux.net>
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*/

/* libc standard */
#include <string.h>

/* Non-standard library */
#include
<caca0.h> /* libcaca-dev */

void msg1() {
    int width = caca_get_width();
   
    caca_set_color(CACA_COLOR_RED, CACA_COLOR_BLACK);
    caca_draw_thin_box(1, 1, (width - 2), 3);

    int i;
    for (i = 1; i <= width - 2; i++) {
        caca_putstr(i, 2,
" ");
    }

    char *message =
"Hello, World!";
    caca_putstr((width - strlen(message)) / 2, 2, message);
}

Quelldatei msg2.c
/*
*  msg2.c
*  Copyright 2009 Dennis Schulmeister <dennis()ncc-1701a.homelinux.net>
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*/


/* libc standard */
#include <string.h>

/* Non-standard library */
#include
<caca0.h> /* libcaca-dev */

char *message1 = "Press ESC to exit";

void msg2() {
    int width = caca_get_width();
   
    caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLUE);
    caca_draw_thin_box(3, 5, (width - 6), 3);

    int i;
    for (i = 4; i <= width - 5; i++) {
        caca_putstr(i, 6,
" ");
    }

    caca_putstr((width - strlen(message)) / 2, 6, message1);
}

Das neue Rahmenprogramm sieht dann so aus:

Quelldatei with_libs.c
/*
*  with_libs.c
*  Copyright 2009 Dennis Schulmeister <dennis()ncc-1701a.homelinux.net>
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*/

/* libc standard */
#include <string.h>

/* Non-standard library */
#include
<caca0.h> /* libcaca-dev */

/* Our own includes */
#include "msg1.h"
#include "msg2.h"

int main(int argc, char** argv) {
    
/* Set up a display to draw upon */
    if (caca_init()) {
        return 1;
    }

    
/* Draw something */
    {
        int width  = caca_get_width();
        int height = caca_get_height();

        
/* Background */
        caca_set_color(CACA_COLOR_GREEN, CACA_COLOR_GREEN);
        caca_clear();

        
/* First message */
        msg1();

        
/* Second message */
        msg2();

        
/* Show it */
        caca_set_window_title(message1);
        caca_refresh();
    }

    
/* Wait for ESC key */
    {
        int quit = 0;
        while (!quit) {
            int event;
            while ((event = caca_get_event(CACA_EVENT_KEY_PRESS))) {
                if ((event & 0xffff) == CACA_KEY_ESCAPE) {
                    quit = 1;
                }
            }
        }
    }

    
/* Clean up */
    caca_end();
    return 0;
}

Das gesamte Paket kann hier heruntergeladen werden,


Zurück zum übergeordneten Thema Build Systeme.


attachments

imageappend Append an Image
>