{"id":6486,"date":"2015-04-17T20:00:52","date_gmt":"2015-04-18T03:00:52","guid":{"rendered":"https:\/\/blog.digilentinc.com\/?p=6486"},"modified":"2015-08-03T17:07:09","modified_gmt":"2015-08-04T00:07:09","slug":"libraries","status":"publish","type":"post","link":"https:\/\/digilent.com\/blog\/libraries\/","title":{"rendered":"Libraries"},"content":{"rendered":"<p>Welcome back to the Digilent Blog!<\/p>\n<p>&nbsp;<\/p>\n<p>This week happens to be National Library week, and although Digilent is not a library, we certainly use them! I&#8217;m not referring to public libraries, though (although I am personally a fan of them); rather, I&#8217;m talking about the C++ libraries that are widely used with\u00a0our microcontrollers. Although C++ libraries are not quite the same as an <a title=\"What Is an IP and How Do You Create One?\" href=\"https:\/\/digilent.com\/blog\/index.php\/what-is-an-ip-and-how-do-you-create-one\/\" target=\"_blank\">IP<\/a>\u00a0for FPGAs, they serve a similar purpose. These libraries include a set of predefined functions that perform a specific task, such as sending and receiving a set of data over SPI, that the user can use without having to define what they do.<\/p>\n<p>&nbsp;<\/p>\n<p>There are two main components that make up a C++ library: a source file and a header file. The header file (a .h file) lists of the functions that will be available for use as well as any constants that might need to be used within the library; it is the file that we include with our main program so that we are able to access the library. The source file (generally a .cpp file) defines what each of the functions will actually do in code and is set up in a very similar style to how users may create their own functions within their main program. You can check out an example of how this is done in our <a title=\"Multiplexer Guessing Game Learn Module\" href=\"http:\/\/learn.blog.digilentinc.com\/Documents\/292\" target=\"_blank\">Multiplexer Guessing Game Learn Module<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<p>In general, users want functions that are associated with a particular library to only ever be accessible when they specifically call them.\u00a0Otherwise, you may have multiple libraries that share a function name such as begin() that all do slightly different things. There would be no way for the compiler to tell which particular begin() you wanted to use. To get around this, C++ libraries each have what is called a <a title=\"What Is a \u201cClass\u201d?\" href=\"https:\/\/digilent.com\/blog\/index.php\/what-is-a-class\/\" target=\"_blank\">class<\/a>\u00a0associated with them. In layman&#8217;s terms, a class is kind of like a extracurricular club\u00a0&#8212;\u00a0you only get access to the library&#8217;s particular functions when you use a member of their club. Luckily, creating a member of a library&#8217;s class is easy to do. For the <a href=\"https:\/\/digilent.com\/blog\/Products\/Detail.cfm?NavPath=2,401,601&amp;Prod=PMOD-JSTK\">PmodJSTK<\/a>, when you include its library (JSTK.h), the compiler knows that the library has its own class, called JSTK. You can create a member of the JSTK class called myJoyStick by typing:<\/p>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_6487\" aria-describedby=\"caption-attachment-6487\" style=\"width: 601px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/04\/Creating-a-Class-member.png\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-6487\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/04\/Creating-a-Class-member.png\" alt=\"Example of creating a Class member\" width=\"601\" height=\"180\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/04\/Creating-a-Class-member.png 601w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/04\/Creating-a-Class-member-600x180.png 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/04\/Creating-a-Class-member-225x67.png 225w\" sizes=\"auto, (max-width: 601px) 100vw, 601px\" \/><\/a><figcaption id=\"caption-attachment-6487\" class=\"wp-caption-text\">Example of creating a class member.<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>The myJoyStick member now has access to all of the functions within the JSTK library and can use them by typing the name of the function after the member:<\/p>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_6488\" aria-describedby=\"caption-attachment-6488\" style=\"width: 598px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/04\/Class-member-using-a-Class-function.png\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-6488\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/04\/Class-member-using-a-Class-function.png\" alt=\"Example of a Class member using a class function\" width=\"598\" height=\"144\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/04\/Class-member-using-a-Class-function.png 594w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/04\/Class-member-using-a-Class-function-225x54.png 225w\" sizes=\"auto, (max-width: 598px) 100vw, 598px\" \/><\/a><figcaption id=\"caption-attachment-6488\" class=\"wp-caption-text\">Example of a class member using a class function.<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>But how does one actually create a library? If you already know what you want your library to do, it&#8217;s not too hard. We need to type out our two files in a program like <a title=\"Notepad++ home page\" href=\"http:\/\/notepad-plus-plus.org\/\" target=\"_blank\">Notepad++<\/a>\u00a0that will let us save the files in the appropriate file types. Some screenshots of a simple C++ library that I created that adds a numeric value of 3 to whatever you gave as an input parameter are shown below:<\/p>\n<figure id=\"attachment_6489\" aria-describedby=\"caption-attachment-6489\" style=\"width: 601px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/04\/AddingThree-header-file.png\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-6489\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/04\/AddingThree-header-file.png\" alt=\"Example of a header file for a AddingThree library\" width=\"601\" height=\"523\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/04\/AddingThree-header-file.png 720w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/04\/AddingThree-header-file-600x523.png 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/04\/AddingThree-header-file-225x196.png 225w\" sizes=\"auto, (max-width: 601px) 100vw, 601px\" \/><\/a><figcaption id=\"caption-attachment-6489\" class=\"wp-caption-text\">Example of a header file for an AddingThree library.<\/figcaption><\/figure>\n<figure id=\"attachment_6490\" aria-describedby=\"caption-attachment-6490\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/04\/AddingThree-source-file.png\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-6490\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/04\/AddingThree-source-file.png\" alt=\"Example of a source file for a AddingThree library\" width=\"600\" height=\"431\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/04\/AddingThree-source-file.png 886w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/04\/AddingThree-source-file-600x431.png 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/04\/AddingThree-source-file-225x162.png 225w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/04\/AddingThree-source-file-800x574.png 800w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-6490\" class=\"wp-caption-text\">Example of a source file for an AddingThree library.<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>Now go and create your own library&#8230;or go visit the library.<\/p>\n<div class='watch-action'><div class='watch-position align-left'><div class='action-like'><a class='lbg-style6 like-6486 jlk' data-task='like' data-post_id='6486' data-nonce='ee750c7abc' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Like' \/><span class='lc-6486 lc'>0<\/span><\/a><\/div><div class='action-unlike'><a class='unlbg-style6 unlike-6486 jlk' data-task='unlike' data-post_id='6486' data-nonce='ee750c7abc' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Unlike' \/><span class='unlc-6486 unlc'>0<\/span><\/a><\/div><\/div> <div class='status-6486 status align-left'>Be the 1st to vote.<\/div><\/div><div class='wti-clear'><\/div>","protected":false},"excerpt":{"rendered":"<p>This week happens to be National Library week, and although Digilent is not a library, we certainly use them! I&#8217;m not referring to public libraries, though (although I am personally a fan of them); rather, I&#8217;m talking about the C++ libraries that are widely used with our microcontrollers. Although C++ libraries are not quite the same as an IP for FPGAs, they serve a similar purpose. These libraries include a set of predefined functions that perform a specific task, such as sending and receiving a set of data over SPI, that the user can use without having to define what they do.<\/p>\n","protected":false},"author":17,"featured_media":6497,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1563],"tags":[],"ppma_author":[4469],"class_list":["post-6486","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-guide"],"jetpack_featured_media_url":"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/04\/NLW-libaries.jpg","authors":[{"term_id":4469,"user_id":17,"is_guest":0,"slug":"jamescolvin","display_name":"James Colvin","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/509febded809c5de0423909f6ae6587f?s=96&d=mm&r=g","author_category":"","user_url":"","last_name":"Colvin","last_name_2":"","first_name":"James","first_name_2":"","job_title":"Applications Engineer \/ Technical Support Engineer \/ Product Support Engineer \/ Technical Writer \/ the person to bother about T&M and JTAG when the senior design engineer is busy","description":"A local Digilent employee who is sometimes tricked into making other content besides documentation and supporting customers on the Digilent Forum, but then I get to write a little more informally so that's a plus. \r\n\r\nA sassy engineer, lover of puns and dad jokes, father and husband. \r\n\r\nI know both way too much and simultaneously almost nothing about a number of nerdy topics. If you want to hear me rant, ask me what data rate USB C operates at."}],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/6486","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/comments?post=6486"}],"version-history":[{"count":0,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/6486\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media\/6497"}],"wp:attachment":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media?parent=6486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/categories?post=6486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/tags?post=6486"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=6486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}