Practical Guide – Building and Using a Static Library (Single Folder Setup)
Heya! 👋 I love helping people, and one of the best ways I do this is by sharing my knowledge and experiences. My journey reflects the power of growth and transformation, and I’m here to document and share it with you.
I started as a pharmacist, practicing at a tertiary hospital in the Northern Region of Ghana. There, I saw firsthand the challenges in healthcare delivery and became fascinated by how technology could offer solutions. This sparked my interest in digital health, a field I believe holds the key to revolutionizing healthcare.
Determined to contribute, I taught myself programming, mastering tools like HTML, CSS, JavaScript, React, PHP, and more. But I craved deeper knowledge and practical experience. That’s when I joined the ALX Software Engineering program, which became a turning point. Spending over 70 hours a week learning, coding, and collaborating, I transitioned fully into tech.
Today, I am a Software Engineer and Digital Health Solutions Architect, building and contributing to innovative digital health solutions. I combine my healthcare expertise with technical skills to create impactful tools that solve real-world problems in health delivery.
Imposter syndrome has been part of my journey, but I’ve learned to embrace it as a sign of growth. Livestreaming my learning process, receiving feedback, and building in public have been crucial in overcoming self-doubt. Each experience has strengthened my belief in showing up, staying consistent, and growing through challenges.
Through this platform, I document my lessons, challenges, and successes to inspire and guide others—whether you’re transitioning careers, exploring digital health, or diving into software development.
I believe in accountability and the value of shared growth. Your feedback keeps me grounded and motivated to continue this journey. Let’s connect, learn, and grow together! 🚀
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:
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 implementationsmain.h→ function prototypesmain.c→ program that will use the library
🟩 Step 2: Write Your Header File (main.h)
#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:
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:
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).
ar -rc libmy.a 0-strlen.o 1-puts.o 2-isalpha.o 3-isdigit.o _putchar.o
Now you have:
libmy.a
👉 This is your static library.
🟩 Step 5: Index the Library
ranlib libmy.a
👉 This allows the compiler to find functions inside the library quickly.
🟩 Step 6: Check What’s Inside the Library (Optional)
ar -t libmy.a
You should see:
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
#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:
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
libmy.a → -lmy
Remove:
lib.a
🟩 Step 9: Run Your Program
./myprogram
Output:
Hello from my static library!
🟦 What Just Happened (Behind the Scenes)
When you ran:
gcc main.c -L. -lmy
The compiler:
Saw
_puts()in your codeLooked inside
libmy.aFound
_putsCopied its compiled code into your program
👉 Only the functions you use are included.
🟦 Important Insight
Even if your library contains:
50 functions
Your program might only include:
1 function
This keeps your executable efficient.
🟦 Clean Up (Optional but Recommended)
Remove .o files after building the library:
rm *.o
Your folder becomes:
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.
library not found
👉 Fix:
-L.
❌ Wrong library name
-lmy ✅ correct
-llibmy ❌ wrong
❌ Forgetting ranlib
Sometimes causes linking issues.
Always run:
ranlib libmy.a
❌ Including main.o in library
👉 Never include your main program in the library.
🟦 Practice (Without Solving Project Directly)
Try this:
Add 2–3 more functions to your
.cfilesRebuild your library
Write a program that uses only ONE of those functions
Compile and run
👉 Observe: only the used function is included.
🟦 Final Mental Model
Think of it like this:
Functions → compiled → packaged → linked → executable
🟦 Key Takeaways
Static libraries bundle compiled functions into one file
You compile
.c → .o → .aYou link using:
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.