What Do C Static Libraries

Let’s dive into a short bit of C source code:

#include <stdio.h>int main(void)
{
printf("Hello, World\n");
return (0);
}

What Are Header Files and What Are Libraries

Header files, such as stdio.h, contain function declarations and macro definitions to be shared between source files. They tell the compiler how to call some functionality, without knowing how the functionality works.

  • Dynamic (to be discussed at a later time)

What Do C Static Libraries Have in Common With Your Kitchen

Imagine that your family likes to eat jerk chicken, meat lasagna, and pumpkin pie every week. They are picky eaters and you make it the same way every week because you know that the recipe works.

  • You don’t have to keep rewriting the same functions that you use frequently.
  • They are great for reusability, as well as collaborating with other programmers.
  • If your object files are in a library and indexed, fewer files need to be searched and opened, and access is faster.

How Do Static Libraries Work

Recall the four steps of compilation.

  1. Compilation: pre-processed code is converted to assembly code
  2. Assembly: code is converted to machine code (zeros and ones), also known as object code
  3. Linking: produces the executable

How to Create Static Libraries

  1. Create a header file with your function prototypes and include the header file in your C source files.
#ifndef WENDYBLOGTECH_H
#define WENDYBLOGTECH_Hint _putchar(char c);
int _isalpha(int c);
int _abs(int n);
int _strlen(char *s);
void _puts(char *s);
char *_strcpy(char *dest, char *src);
char *_strcat(char *dest, char *src);
char *_strncat(char *dest, char *src, int n);
int _strcmp(char *s1, char *s2);
char *_memcpy(char *dest, char *src, unsigned int n);
char *_strstr(char *haystack, char *needle);#endif
gcc -Wall -pedantic -Werror -Wextra -c *.c
0-strcat.o
1-memcpy.o
1-strncat.o
2-strlen.o
2-strncpy.o
3-puts.o
3-strcmp.o
4-isalpha.o
5-strstr.o
6-abs.o
_putchar.o
ar -rc <libraryname.a> *.oexample: ar -rc libwendyblogtech.a *.o
ar -t <libraryname.a>example: ar -t libwendyblogtech.a
ranlib <libraryname.a>example: ranlib libwendyblogtech.a
$ nm libwendyblogtech.a0-strcat.o:
0000000000000000 T _strcat
1-memcpy.o:
0000000000000000 T _memcpy
1-strncat.o:
0000000000000000 T _strncat
2-strlen.o:
0000000000000000 T _strlen
2-strncpy.o:
0000000000000000 T _strncpy
3-puts.o:
U _putchar
0000000000000000 T _puts
3-strcmp.o:
0000000000000000 T _strcmp
4-isalpha.o:
0000000000000000 T _isalpha
5-strstr.o:
0000000000000000 T _strstr
6-abs.o:
0000000000000000 T _abs
_putchar.o:
0000000000000000 T _putchar
U write

How to Use Static Libraries

Now that we have our static library, let’s invoke the library after writing a program that uses the _puts function from the library.

#include "wendyblogtech.h"int main(void)
{
_puts("\"That's how you create a static libary.\"");
return (0);
}
Example of invoking libwendyblogtech.a:gcc quote.c -L. -lwendyblogtech -o quote
"That's how you create a static library."