Building libraries using Open Watcom
04 Oct 2015Being able to bundle blocks of your code (and data to some extent) into library files is quite a productive step forward when developing applications. Being able to port these pieces around means a higher level of code-reuse, and a less number of times you’ll spend re-writing the same stuff.
In today’s post, I’ll take you through creating a very minimal library. We’ll create a library module from this code and I’ll also show you how to consume it.
Howdy!
Our example library will expose one function, called greet
. greet
will take in a person’s name and will print a greeting to the console. Here’s the header:
The implementation is basic. It doesn’t even really matter, but is included for completeness:
Make me a library
Making a library is all about compiling your code to produce object files and then bundling your object files into a library file. So, the first step is to compile greeting.c
into an object file:
After this, you’ll now have GREETER.OBJ
in your project folder. You can turn this into a library with the following:
The command itself says invoke wlib
to create (or modify) a library called greeter
(the .lib
extension is handled for us). Finally the +greeter
says that we want to add greeter.obj
into the library. We’ll now have a .LIB
file that we can link against.
Consuming the library
Writing code that actually uses the library is as easy as including the header and calling functions. Here’s a test:
Converting this into a callable executable is achieved with `wcl386’.
That’s all there is to it.