Fork me on GitHub

Project Notes

C++ Static Libraries

Basics of building and using static libraries with C++.


Distributing code as static libraries for C++:

  • library maker produces the binary library files with associated header files
  • library user:
    • compiles the program with the library header files
    • links the program with the library binary files

The “static” reference means the library references are resolved at compile/link time, so the resulting executable has includes the library built-in. i.e. no runtime dependency on the library binaries.

Making the Library

The animals folder defines a simple library. It provides a “Cat” class that knows how to speak().

The Makefile is setup to generate a library file. Basically, instead of targeting an executable like an actual program, we compile an link to a library archive file. Since I am on MacOS, I’m using the GCC archiver

$ cd animals
$ make
c++ -std=c++17 -Wall -O3   -c -o cat.o cat.cpp
ar -rsc libanimals.a cat.o

The library follows Static Libraries conventions:

"lib" + library-name + ".a"

So given our library is called “animals”, the library file is “libanimals.a”

NB: Shared Libraries conventions:

"lib" + library-name + ".so"

Distributing the library

For others to use the library they need to things - the compiled library, and header files. For this test, I’m simply going to “distribute” using cp(!) …

$ cp animals/libanimals.a demo/lcklib/
$ cp animals/*.h demo/lcklib/include

NB: I’ve arbitrarily decided to put the binaries in a lcklib folder, and headers in lcklib/include.

Using the Library

For client programs to use the library, they need two things:

  • compiler needs to be told where to find the header files (-I./lcklib/include)
  • the linker needs to be told where to find the binaries (-L./lcklib) and what libraries to link (-lanimals)

The Makefile is setup to do that for us:

$ cd demo
$ make
c++ -std=c++17 -Wall -O3 -I./lcklib/include   -c -o demo.o demo.cpp
c++ -L./lcklib -lanimals  demo.o   -o demo
Hello there!!!

Credits and References

About LCK#244 C++
Project Source on GitHub Return to the Project Catalog

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.