{"id":107,"date":"2014-11-05T08:43:41","date_gmt":"2014-11-05T08:43:41","guid":{"rendered":"http:\/\/www.troliver.com\/?p=107"},"modified":"2015-01-02T20:15:08","modified_gmt":"2015-01-02T20:15:08","slug":"howto-visual-studio-libraries-and-environment-variables","status":"publish","type":"post","link":"https:\/\/www.troliver.com\/?p=107","title":{"rendered":"Howto: Visual Studio, Libraries and Environment Variables"},"content":{"rendered":"<p>There are a few typical scenarios that I see come up in classes every year for programming students who are trying to use APIs and libraries that are not included by default in Microsoft\u2019s Visual Studio installation;<\/p>\n<ul>\n<li>Trying to \u201cinstall\u201d OpenGL<\/li>\n<li>Trying to link external libraries to their project.<\/li>\n<li>Running programs that use external libraries.<\/li>\n<li>Understanding what IDE, API, libraries, DLL and environment variables even mean.<\/li>\n<\/ul>\n<p>This post aims to try and help people to understand a few differences between different these concepts and generally make it simpler to manage your code stuff. If anyone has any questions, post it in the comments section so that I can try and modify this to make more sense. This might seem simplistic at times to a degree that makes certain concepts almost sound inaccurate in their description; but the point is that a range of people can understand the basic concepts and get things up and running as soon as possible. First off, a couple of explanations of terminologies:<\/p>\n<ul>\n<li><strong>IDE<\/strong>: Integrated Developer\/Development Environment. When you code, you write a load of text. When you compile, you run a program to convert that text into something else (usually a binary executable file on Windows, a .exe file). These are two separate things you would do and, along with debugging and a range of other things you might want to do, thankfully there are programs out there that wrap it all up together in one program. This is what an IDE is. Visual Studio and Eclipse are a couple of examples of this.<\/li>\n<li><strong>API<\/strong>: Basically just a way to access (in other words, an interface to) various libraries. You would download a <em>library<\/em> and access it through an\u00a0<em>API<\/em>. If you installed Visual Studio, you already have access to the Win32 libraries through the Win32 API. To use it, you would just include the relevant headers (.h files) and call the different\u00a0functions. So an\u00a0API provides a way for someone to use already-developed code (libraries) in their own code. APIs you might commonly use will\u00a0often\u00a0come with some sort of documentation somewhere that describes each of the functions and some example usage. The scripting that you can do in Unity, and any programming you do with OGRE, use the Unity and OGRE APIs.<\/li>\n<li><strong>Middleware<\/strong>, in the context of software development, is\u00a0some software\u00a0that bridges various gaps in development. So, for example, you might have a system that implements graphics APIs, or handles all the audio for you or focuses on different inputs you might have from various game controllers and interface devices. Middleware can often be cross-platform, so you don&#8217;t have to worry about underlying implementations for different systems. It is usually used to describe a complete software package, so you could consider things like OGRE and Unity to be middleware as you can run your game on top of these engines.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>OpenGL libraries<\/h2>\n<p><em>e.g. GLFW, GLEW and so on<\/em><\/p>\n<p>So you have Visual Studio set up and you want to use some obscure OpenGL library. But you haven\u2019t installed OpenGL and when you have downloaded any\u00a0libraries, it isn&#8217;t really clear how to install them.<\/p>\n<p>For the sake of simplicity, the first thing to make sure you do is that, when downloading libraries, you find the\u00a0<em>binary<\/em> rather than the\u00a0<em>source<\/em>. A binary has been pre-compiled already &#8211; which means that you can use it straight away so long as you are using the right binary. For example, if you download the Mac version of a library, it won&#8217;t work on Windows. This is what the source is useful for &#8211; compilation yourself on a specific system.<\/p>\n<p>But, luckily, Visual Studio 2013 binaries for 64 bit (and 32 bit) Windows are fairly standard, so you can go ahead and probably just download the binary with no issue. However, from there you have to move a few files around.<\/p>\n<h3>Headers, Libraries and DLL files<\/h3>\n<p>The three files you&#8217;ll be interested in, in the binary packages, are:<\/p>\n<ul>\n<li>DLL (dynamically linked library) files which contain, basically, code that has already been written which does certain stuff. This is a\u00a0<strong>runtime<\/strong>\u00a0file and is used by your executables that you have already built.<\/li>\n<li>Headers &#8211; contains function definitions for use\u00a0when you are programming. A header file (.h) will contain references to functions that are included in..<\/li>\n<li>Libraries (.lib) files. This is essentially a big load of code packed into a single file. Hence it being called a library. You can&#8217;t really see this code yourself, so you have to use a header to access it. You&#8217;ll get errors during <strong>compile time\u00a0<\/strong>if it can&#8217;t find the right library associated with the functions you have written that are referenced in the header. You might be using a 64 bit library, for example, on a 32 bit compilation or vice versa. Or you might not have linked your program to that library.<\/li>\n<\/ul>\n<p>So essentially you use your .h and your .lib files when developing a program and your .dll file when distributing it. Lets see how you actually &#8220;install&#8221; these on your system.<\/p>\n<ul>\n<li>Download your library you want from wherever. For example GLFW (http:\/\/www.glfw.org\/download.html and click either the <strong>32 bit<\/strong> or <strong>64 bit binaries\u00a0<\/strong>buttons. You only need one but it will kind of make some difference later. Go for 32 bit maybe).<\/li>\n<li>I will go ahead and assume you are using Visual Studio 2013 (which is called Visual Studio 12 &#8211; just go with this. If you are using 2012, use Visual Studio 11) and you are running a 64 bit windows. The first thing to do is to navigate to your\u00a0<strong>Visual C<\/strong> folder, which would be located in this example here:<\/li>\n<\/ul>\n<pre style=\"padding-left: 90px;\">C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\<\/pre>\n<ul>\n<li>Inside this folder are two other folders;\u00a0<em>lib<\/em> and\u00a0<em>include<\/em>. Using the example of GLFW, you would put glfw3.h into\u00a0<em>include<\/em> and glfw3.lib into\u00a0<em>lib<\/em><\/li>\n<li>Next you want to find your system folder. On 64 bit windows, this is C:\/Windows\/SysWOW64. Inside here, you want to put your dll files &#8211; this can actually go into a number of places, but system files here are a good place to put your dll files that can be used by numerous other applications.<\/li>\n<li>Finally, boot up your program and either add a reference to your library (eg glfw3.lib) to your project properties (<em>Project -&gt; &lt;application name&gt; Properties -&gt; Configuration Properties -&gt; Linker -&gt; Input<\/em>) under the &#8220;Additional Dependencies&#8221; section or do it inline in your code. I prefer the code way, because this is much more visible to the user. Do that through doing:<\/li>\n<\/ul>\n<pre style=\"padding-left: 90px;\">#pragma lib (lib, \"glfw3.lib\")<\/pre>\n<p>Now you will be linking against this library when you compile your code. Just make sure you also do an <em>#include glfw3.h <\/em>for the above example and\u00a0you&#8217;re done!<\/p>\n<p>Some issues you might run into with those three parts:<\/p>\n<ul>\n<li><strong>Coding<\/strong>: If you don&#8217;t include the\u00a0<em>#include glfw3.h<\/em>, you will get errors when you are, such as undefined functions.<\/li>\n<li><strong>Compiling<\/strong>: If\u00a0you don&#8217;t include\u00a0<em>#pragma comment\u00a0(lib, &#8220;glfw3.lib&#8221;),\u00a0<\/em>you will get errors\u00a0such as unresolved external symbol errors (note: this also occurs when you are using a 32\/64 bit binary on a different target build, e.g. a 64 bit binary for a 32 bit build. This is easiest fixed by just redownloading the other binary that you didn&#8217;t download)<\/li>\n<li><strong>Runtime<\/strong>:\u00a0If you don&#8217;t include your\u00a0<em>glfw3.dll<\/em> in your system folder, or you are distributing your application to someone else later and they don&#8217;t have the DLL themselves, you will get an error when the application runs<strong>.\u00a0<\/strong>The application will just not run and you will get an error saying that the DLL can&#8217;t be found. Just make sure the DLL is in your system and, if distributing your application, include this in the same directory as your executable file.<\/li>\n<\/ul>\n<p>I&#8217;ll probably update this post and tidy it a bit later, but if anyone has a question, just leave it in the comments section!<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are a few typical scenarios that I see come up in classes every year for programming students who are trying to use APIs and libraries that are not included by default in Microsoft\u2019s Visual Studio installation; Trying to \u201cinstall\u201d OpenGL Trying to link external libraries to their project. Running programs that use external libraries. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_newsletter_tier_id":0,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[21],"tags":[22,5],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6PQZ3-1J","_links":{"self":[{"href":"https:\/\/www.troliver.com\/index.php?rest_route=\/wp\/v2\/posts\/107"}],"collection":[{"href":"https:\/\/www.troliver.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.troliver.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.troliver.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.troliver.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=107"}],"version-history":[{"count":5,"href":"https:\/\/www.troliver.com\/index.php?rest_route=\/wp\/v2\/posts\/107\/revisions"}],"predecessor-version":[{"id":158,"href":"https:\/\/www.troliver.com\/index.php?rest_route=\/wp\/v2\/posts\/107\/revisions\/158"}],"wp:attachment":[{"href":"https:\/\/www.troliver.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.troliver.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.troliver.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}