Building Static Libraries

Resourceful Code Reuse — by Dmitry Zinoviev (15 / 20)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Compiling Object Files | TOC | Building Dynamic Libraries 👉

A well-debugged, well-tested, and well-documented object file with several functions and global variables is an excellent reusable code unit. You can link it with your program to get access to the previously implemented features. Yet, there are two practical considerations related to object files:

  1. You should collect the relevant files in an archive and give it a descriptive name.
  2. Some people consider it inelegant to have more than one public function per file. In fact, in Java, it’s explicitly forbidden to have more than one public class per file. If you try to avoid having more than one public class or function per file, you may end up compiling several object files that together implement the desired functionality. You’ll have to tell the linker to link all of them. The more files you have, the more you tell the linker until you forget why all those objects files are on the linker’s command line. This is also true of the file names that probably match the names of the public functions, but not the name of the problem they collectively solve.
  3. Linking a descriptively named archive, rather than individual object files, will take some pressure off you.
  4. You should store that archive someplace where the linker can find it without your assistance. Notifying the linker about the locations of…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.