# Practical Guide – Building and Using a Static Library (Single Folder Setup)

So far, you’ve learned:

*   Why libraries exist
    
*   What static libraries are
    
*   How they are built conceptually
    
*   The tools involved
    
*   How linking works
    

Now we bring everything together.

> In this lesson, you will **build and use a static library step by step** using a simple setup — all files in one directory.

No complex folder structures. Just the essentials.

## 🟦 Scenario

You have multiple functions you’ve written before, like:

*   `_strlen`
    
*   `_puts`
    
*   `_isalpha`
    
*   `_isdigit`
    

Instead of copying them into every program, you want to:

> Combine them into a reusable static library and use them in another program.

## 🟩 Step 1: Your Starting Files (All in One Folder)

Your directory looks like this:

```id="q6ozh4"
0-strlen.c
1-puts.c
2-isalpha.c
3-isdigit.c
_putchar.c
main.h
main.c
```

## 🔹 What each file does:

*   `.c files` → your function implementations
    
*   `main.h` → function prototypes
    
*   `main.c` → program that will use the library
    

## 🟩 Step 2: Write Your Header File (`main.h`)

```c
#ifndef MAIN_H
#define MAIN_H

int _strlen(char *s);
void _puts(char *s);
int _isalpha(int c);
int _isdigit(int c);
int _putchar(char c);

#endif
```

👉 This file tells the compiler what functions exist.

## 🟩 Step 3: Compile All `.c` Files into `.o` Files

Run:

```bash
gcc -Wall -Werror -Wextra -pedantic -std=gnu89 -c *.c
```

## ⚠️ Important Note

This will also compile `main.c` into `main.o`.

That’s fine — we just won’t include it in the library.

## After this step, you get:

```id="s0p0z1"
0-strlen.o
1-puts.o
2-isalpha.o
3-isdigit.o
_putchar.o
main.o
```

## 🟩 Step 4: Create the Static Library

Now we bundle ONLY the function object files (not `main.o`).

```bash
ar -rc libmy.a 0-strlen.o 1-puts.o 2-isalpha.o 3-isdigit.o _putchar.o
```

#### Now you have:

```id="2e0m6x"
libmy.a
```

👉 This is your static library.

## 🟩 Step 5: Index the Library

```bash
ranlib libmy.a
```

👉 This allows the compiler to find functions inside the library quickly.

## 🟩 Step 6: Check What’s Inside the Library (Optional)

```bash
ar -t libmy.a
```

You should see:

```id="g4l0w0"
0-strlen.o
1-puts.o
2-isalpha.o
3-isdigit.o
_putchar.o
```

## 🟩 Step 7: Write a Program That Uses the Library

`main.c`

```c
#include "main.h"

int main(void)
{
    _puts("Hello from my static library!");
    return 0;
}
```

## 🟩 Step 8: Compile and Link the Library

Now the **most important command**:

```bash
gcc main.c -L. -lmy -o myprogram
```

#### 🔍 Breakdown

| Part | Meaning |
| --- | --- |
| `main.c` | your program |
| `-L.` | look for libraries in current directory |
| `-lmy` | link `libmy.a` |
| `-o myprogram` | output executable |

#### 🔑 Key Rule

```text
libmy.a → -lmy
```

Remove:

*   `lib`
    
*   `.a`
    

## 🟩 Step 9: Run Your Program

```bash
./myprogram
```

Output:

```id="dzbqpl"
Hello from my static library!
```

## 🟦 What Just Happened (Behind the Scenes)

When you ran:

```bash
gcc main.c -L. -lmy
```

The compiler:

1.  Saw `_puts()` in your code
    
2.  Looked inside `libmy.a`
    
3.  Found `_puts`
    
4.  Copied its compiled code into your program
    

👉 Only the functions you use are included.

## 🟦 Important Insight

Even if your library contains:

```id="rhrw4o"
50 functions
```

Your program might only include:

```id="l7n1m3"
1 function
```

This keeps your executable efficient.

## 🟦 Clean Up (Optional but Recommended)

Remove `.o` files after building the library:

```bash
rm *.o
```

Your folder becomes:

```id="ib2v61"
0-strlen.c
1-puts.c
2-isalpha.c
3-isdigit.c
_putchar.c
main.h
main.c
libmy.a
myprogram
```

Clean and organized.

## 🟦 Common Mistakes (Very Important)

### ❌ Forgetting `-L.`

```text
library not found
```

👉 Fix:

```bash
-L.
```

### ❌ Wrong library name

```bash
-lmy   ✅ correct
-llibmy ❌ wrong
```

### ❌ Forgetting `ranlib`

Sometimes causes linking issues.

Always run:

```bash
ranlib libmy.a
```

### ❌ Including `main.o` in library

👉 Never include your main program in the library.

* * *

## 🟦 Practice (Without Solving Project Directly)

Try this:

1.  Add 2–3 more functions to your `.c` files
    
2.  Rebuild your library
    
3.  Write a program that uses only ONE of those functions
    
4.  Compile and run
    

👉 Observe: only the used function is included.

## 🟦 Final Mental Model

Think of it like this:

```text
Functions → compiled → packaged → linked → executable
```

## 🟦 Key Takeaways

*   Static libraries bundle compiled functions into one file
    
*   You compile `.c → .o → .a`
    
*   You link using:
    

```bash
gcc main.c -L. -lmy -o program
```

*   Only used functions are included
    
*   Your final program becomes self-contained
    

## What’s Next

Now that you understand the full workflow, you are ready to:

*   Build your own static library for the project
    
*   Organize your functions properly
    
*   Follow the required rules
    

This is one of the most important steps toward writing **modular and reusable C code**.
