This is an example of creating your own library to use that has some status variables only available locally to it.
Remarks:
- It is written as an idea from where you can draw parallel and start building up your own libraries. It is meant to give inspiration for a reusable logging library that you can later on use in your Homework 2.
- Technically, it is not a library, but only because we are not compiling and preparing it as such. However when compiled differently, it can be a library that can be given to anyone.
- The code is compiled and bundled with your executable, so it still increases the binary file size.
- More on library creation, including how to create static and dynamic libraries, as well as compile them in a way that they wouldn’t be a part of your main binary: http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html
Main code with example commands. It also contains the inclusion of the header file, which will tell the compiler what functions are available and help the IDE use them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <stdio.h> #include "writer.h" int main(void) { int val; SetValue(5); WriteUpdate(); WriteUpdate(); SetCoefficient(-3); WriteUpdate(); WriteUpdate(); val = GetValue(); printf("The value is %d\n", val); return 0; } |
Library header
Add all the content that you need for the header. This is what will be shown to “the rest of the world” as things that are available in your library. It can also be called an interface in a way.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#ifndef WRITER_H #define WRITER_H #include <stdio.h> #include <stdbool.h> void SetValue(int init); int GetValue(void); void SetCoefficient(int coef); int WriteUpdate(void); #endif |
Library code
This is where you will implement your code. All the functionality that you promised in the header must be available here.
You will also see global variables here. Usually we try to avoid these, but that’s not always necessary. There are 2 things to note here. First, it will avoid us having to carry some general information around, that should be available for every part of the program. Secondly, these are localized to the function – we are not interacting with it outside of the predefined set of functions. You cannot alter the values outside of this file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
#include "writer.h" int value; int coefficient = 1; // default is 1 bool valueSet = false; void SetValue(int init) { value = init; valueSet = true; } int GetValue(void) { if (valueSet) return value; else return 0; } void SetCoefficient(int coef) { coefficient = coef; } int WriteUpdate(void) { if (!valueSet) { fprintf(stderr, "Value not set!!\n"); return -1; } value *= coefficient; FILE *fp = fopen("out.txt", "a"); if (fp != NULL) { fprintf(fp, "%d\n", value); fclose(fp); } else { fprintf(stderr, "Opening output failed!\n"); return -2; } return 0; } |
To compile the project:
gcc -Wall -Wextra -Wconversion demo.c writer.c -o demo