Creating your own Libraries

Don't bury generally useful code in an application program. Make as much functionality as possible available via C library functions, so users can access it from both the command-line and from within programs written in any language. Applications should largely be command-line or GUI interfaces to functionality found in the libraries.

E.g. if writing a program that converts all characters in a file to upper case:

shell-prompt: filetoupper < input.txt > output.txt
        

Base it on C library functions that do the same. This way, you and others can easily perform this operation from within a C program as easily as from the Unix command line. The program itself becomes a trivial wrapper around the function:

#include <stdio.h>
#include <mylibheader.h>

int     main()

{
    return filetoupper(stdin, stdout);
}
        

The library function:

#include <stdio.h>
#include <errno.h>

int     filetoupper(FILE *infile, FILE *outfile)

{
    int     ch;
    
    while ( (ch = getc(infile)) != EOF )
        putc(outfile, toupper(ch));
    
    return errno;
}
        

Static:

ar -rs libmystuff.a *.o
        

Dynamic:

cc -shared -o libmystuff.so *.o
        
cc myprog.c -o myprog -Lparent-dir-of-library -lmystuff