A Trip to the C Library. Creating Static Libraries

“For whosoever reads this, immense power will bestow them… probably.”

The shallow version:

To compile all your C files in the directory to object files:

gcc -c *.c

To create a library:

ar -rc yourlibraryname.a *.o

Then index your library:

ranlib yourlibraryname.a

The in-depth version:

We use libraries to organize information, which often simplifies access to that information. The same can be said for C libraries, in which a library could house many small functions and scripts, rather than having a huge collection of files in a directory.

Once the library is built, the program main.o can be compiled to an executable with the following command:

cc main.o -L. -lyourlibraryname -o prog

where -L tells the linker to look for the library in the current directory and where -l indicates that this is the name of the library.

But what about the “ranlib” command?

The ranlib command is used to index the library so that symbol look-up is faster in the library. It’s easier to find a book in a shelf when the collection is in alphabetical order. It’s similar with finding code in a collection of scripts.

Finally, please note that libraries are compiled different on mac laptops with M1 chips.




Is currently student at Holberton New Haven

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

Recommended from Medium

Backtracking : TSP

Set up Vertex AI Workbench with access to BigQuery and GCS using Terraform

GitHub Action That Automates Portfolio Generation

Bash Scripting Tutorial — part 10

4 Weird Built-in Python Data Structures & What They Do

· Easy and custom Time Tracker Tool for daily use

13 Useful Python Snippets That You Need to Know

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
Thomas Francis

Thomas Francis

Is currently student at Holberton New Haven

More from Medium

Bezier Surface Implementation in OpenGL

Transferring Windows Server roles to a new server, Part II FSMO

Database Column Type: DateTime with Microseconds with DBAL

Buffer Overflow || Binary Exploitation || CTF — EASY BUFFER