{"id":5717,"date":"2021-02-11T01:22:20","date_gmt":"2021-02-10T23:22:20","guid":{"rendered":"https:\/\/blue.pri.ee\/ttu\/?page_id=5717"},"modified":"2022-02-20T17:03:38","modified_gmt":"2022-02-20T15:03:38","slug":"creating-a-library","status":"publish","type":"page","link":"https:\/\/blue.pri.ee\/ttu\/programming-ii\/code-samples\/creating-a-library\/","title":{"rendered":"Creating easily reusable code"},"content":{"rendered":"<p>This is an example of creating your own library to use that has some status variables only available locally to it.<\/p>\n<p><strong>Remarks:<\/strong><\/p>\n<ul>\n<li>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.<\/li>\n<li>Technically, it is not a library, <strong>but only because we are <\/strong><strong>not compiling and preparing it as such<\/strong>. However when compiled differently, it can be a library that can be given to anyone.<\/li>\n<li>The code is compiled and bundled with your executable, so it still increases the binary file size.<\/li>\n<li>More on library creation, including how to create static and dynamic libraries, as well as compile them in a way that they wouldn&#8217;t be a part of your main binary: <a href=\"http:\/\/www.yolinux.com\/TUTORIALS\/LibraryArchives-StaticAndDynamic.html\">http:\/\/www.yolinux.com\/TUTORIALS\/LibraryArchives-StaticAndDynamic.html<\/a><\/li>\n<\/ul>\n<p><strong>Main code with example commands.\u00a0<\/strong>It also contains the inclusion of the header file, which will tell the compiler what functions are available and help the IDE use them.<\/p>\n<pre class=\"lang:c decode:true \" title=\"demo.c\">#include &lt;stdio.h&gt;\r\n#include \"writer.h\"\r\n\r\nint main(void)\r\n{\r\n\tint val;\r\n\tSetValue(5);\r\n\tWriteUpdate();\r\n\tWriteUpdate();\r\n\tSetCoefficient(-3);\r\n\tWriteUpdate();\r\n\tWriteUpdate();\r\n\tval = GetValue();\r\n\tprintf(\"The value is %d\\n\", val);\r\n\treturn 0;\r\n}\r\n<\/pre>\n<p><strong>Library header<\/strong><\/p>\n<p>Add all the content that you need for the header. This is what will be shown to &#8220;the rest of the world&#8221; as things that are available in your library. It can also be called an interface in a way.<\/p>\n<pre class=\"lang:c decode:true\">#ifndef WRITER_H\r\n#define WRITER_H\r\n\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdbool.h&gt;\r\n\r\nvoid SetValue(int init);\r\nint GetValue(void);\r\nvoid SetCoefficient(int coef);\r\nint WriteUpdate(void);\r\n\r\n\r\n#endif\r\n<\/pre>\n<p><strong>Library code<\/strong><\/p>\n<p>This is where you will implement your code. All the functionality that you promised in the header must be available here.<\/p>\n<p>You will also see global variables here. Usually we try to avoid these, but that&#8217;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 &#8211; we are not interacting with it outside of the predefined set of functions. You cannot alter the values outside of this file.<\/p>\n<pre class=\"lang:c decode:true \" title=\"writer.c\">#include \"writer.h\"\r\n\r\nint value;\r\nint coefficient = 1; \/\/ default is 1\r\nbool valueSet = false;\r\n\r\nvoid SetValue(int init)\r\n{\r\n\tvalue = init;\r\n\tvalueSet = true;\r\n}\r\n\r\nint GetValue(void)\r\n{\r\n\tif (valueSet)\r\n\t\treturn value;\r\n\telse\r\n\t\treturn 0;\r\n}\r\n\r\nvoid SetCoefficient(int coef)\r\n{\r\n\tcoefficient = coef;\r\n}\r\n\r\nint WriteUpdate(void)\r\n{\r\n\tif (!valueSet)\r\n\t{\r\n\t\tfprintf(stderr, \"Value not set!!\\n\");\r\n\t\treturn -1;\r\n\t}\r\n\tvalue *= coefficient;\r\n\tFILE *fp = fopen(\"out.txt\", \"a\");\r\n\tif (fp != NULL)\r\n\t{\r\n\t\tfprintf(fp, \"%d\\n\", value);\r\n\t\tfclose(fp);\r\n\t}\r\n\telse\r\n\t{\r\n\t\tfprintf(stderr, \"Opening output failed!\\n\");\r\n\t\treturn -2;\r\n\t}\r\n\treturn 0;\r\n}\r\n<\/pre>\n<p>To compile the project:<\/p>\n<p><span class=\"lang:c decode:true crayon-inline \">gcc -Wall -Wextra -Wconversion demo.c writer.c -o demo<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/programming-ii\/code-samples\/creating-a-library\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">Creating easily reusable code<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2361,"menu_order":9,"comment_status":"closed","ping_status":"closed","template":"page-templates\/code-width-wide.php","meta":{"footnotes":""},"class_list":["post-5717","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/5717","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/comments?post=5717"}],"version-history":[{"count":0,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/5717\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/2361"}],"wp:attachment":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/media?parent=5717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}