Thursday, August 15, 2024

How to create an opaque library in C

Or, as I call it, a real library. 

The example code on github.

One of the things about all the libraries is that they are opaque and they encapsulate the functions they provide.  You create an object that is just a handle. They have accessor functions  to let you set or get values so that the object can do the 5 or 6 things internally when that value changes.  You can also have the library do tasks for you and return a status or a result from that action. 

I just converted by neural net library to be opaque and encapsulate all the actions around training and running neural nets. I was getting to the place where just setting a value in the net object wouldn't do all the things that needed done when that object is set.  Sometime memory needs updated or disposed of when the value changes.

Since I had to look up how to properly convert my library to be opaque I decided to write this article to help other people trying to do the same thing.  This is a simple example program that demonstrates all the steps for creating an opaque library for the .h and .c file in the library, as well as an example program that uses the library.

This is the object.h file:



This is the object.c file: 


And this is the test.c file:




Finally, this is the header file for my neural network program:





No comments:

Post a Comment