Anyone who’s programmed in C for a while has worked with other libraries. In Linux, it’s common to link in the math library, libm.a. Other libraries are available to expand what the C language can do. I have books available on Ncurses, curl, XML and JSON, each of which has a library that makes it easy to use these powerful utilities. But what about making your own library?
Anyone who can compile and link C code can roll their own library. I’ve done so a few times, ideally to share common functions I’ve created and make them available to multiple projects — just like any C library. Here are the steps:
- Create a source code file with the functions you want accessed in the library.
- Compile the source code file into an object code file.
- Archive the object code file into a library file.
It’s the archive that becomes the library, allowing your functions to be linked in with other projects.
I work through these steps at the command prompt in a terminal window. I’ve never used an IDE to create a library, though I assume it’s possible given that most IDEs are just shells for command prompt directives anyhow. Here are the details:
Write Source Code. The source code file you create doesn’t use a main() function. Only those functions necessary for the library need to be written. You can include header files, add preprocessor directives, all that. But remember that any defined constants are local to the library. This limitation explains why many libraries also come with their own header files that include necessary defined constants, macros, and other public thingies.
Compile Object Code. To compile the object code use the -c switch, as in:
clang -c source.c
Upon success, an object code file (source.o in this example) is created. No program is built. In fact, none can be when you omit the main() function, the program’s entry point.
Archive the Object Code. The final step involves using the ar utility as follows:
ar -rcs libmine.a source.o
The -r switch adds the file(s) specified to the archive, such as source.o shown above.
The -c switch creates the archive libmine.a.
The -s switch writes an object file index into the archive. This symbol table assists the linker when building the final program.
The library filename can be anything, but I recommend using the same pattern as other C libraries: lib followed by the library name, then .a (archive) for the filename extension.
At this point, the library is ready to be linked with any C project. To do so, use the -l switch followed by the library’s short name. For libmine.a, the switch is: -lmine The linking action is the second step when building a program. Traditionally, the linker (ld) was used after compiling, but modern compilers perform both steps.
Beyond the current directory, the linker looks for library files in the /usr/lib directory and its numerous subdirectories. For your own libraries, the linker also looks in /usr/local/lib, which is where you should put any libraries you plan on using. For specific locations, the -L switch is used to hunt down libraries lurking elsewhere.
Have fun with your libraries!