What Do C Static Libraries

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

What Are Header Files and What Are Libraries

  • Static (we’ll discuss static libraries in this blog post)
  • Dynamic (to be discussed at a later time)

What Do C Static Libraries Have in Common With Your Kitchen

  • As programs grow, using libraries makes it easier to manage code.
  • 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

  1. Preprocessing: removes comments and deals with # directives
  2. Compilation: pre-processed code is converted to assembly code
  3. Assembly: code is converted to machine code (zeros and ones), also known as object code
  4. 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

#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."

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Phase 2: Powerup Magnet

NFT Podcast: Sustainability Inclusivity with NFTs Blue M0xn

Knowledge to be a software architect.

[LeetCode]#977. Squares of a Sorted Array

THRIVING IN A GROWTH ENVIRONMENT

Using Git as Version Control System to Code Together

Final project of Coding, WEB PAGE

Heroku Pipelines: changing the auto-deploy branch from master to main

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Leopoldo Luna

Leopoldo Luna

More from Medium

Mock Objects in TDD

Docker Basics

Azure Authentication in Angular and C#

C#/VB.NET: Insert Superscripts and Subscripts in Word