How-To Use C++ Libraries (without relying on a package manager)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] Thanks time we have a little conversation about how to do libraries and C++ now it's the thing about libraries C++ and just about any other language eventually someone comes along and makes a package manager for them but there is no standard and that's where the problem comes in there are so many different ways to do C++ libraries and it's you know like furred for JavaScript there's node for whatever there's whatever like every language there's something Python has pip I'm trying to think of other one who camel has some something to every language of ever checked out and they got into a little bit always has something you sleep plus plus libraries I searched for this I just get a bunch of C++ libraries are used commonly like boost poco which is a soft word network stop yes io again some of these I'm going to use some of these I have but how do you include them that that's where sometimes people will get stuck and there's a couple different ways you can do a statically you can do it dynamically or you can do some header only build where it builds in with the objects of your project that's the basic ways and to briefly explain the difference between static and dynamic libraries I spelled that wrong but whatever it'll correct there we go so static libraries are copied per project basically when you include a static library you get a copy of the library with whatever you're building when you do a dynamic library the libraries already built and you just use whatever the functions out of it and it's not built with your project so if you have multiple projects and you don't want to copy the library monster times use dynamic if you want it to be fully included on its own with your project to use static both have their advantages and disadvantages but that's the gist of it dynamic will make you end up with smaller object files typically but then you have to include the dll or whoever tries to run your project it's also run time the dynamic is a runtime check so when someone goes to run your program they'll throw that dll error who doesn't find it you've probably seen that before pop-up but a static library will never do that it's already built in but the object file is larger so I guess the header only is sort of like static okay it's not actually but it basically is built-in with your objects just the same so it's not being reused from a single dynamic library there's Conan Conan IO which is a open-source package manager that you can generally use for everything I tried to get started with this I spent about an hour with Conan and I kind of got it going it actually has you do everything through Python which is a kind of cool I guess but it was a little more named for how when you read the answer an introduction it makes it sound like it's super simple and I can see that it kind of is but you've got to understand a lot of things before it gets simple and I was starting to kind of get to the point where it was starting to get simplified for me but overall I'm not the biggest fan in the world yeah I'm still gonna play around with Conan may be able to do a video later about Conan I oh let me know if you'd like to see that but the other one is VC package manager and as you could see someone had as I was typing it in VC package manager verse Conan is a pretty popular thing so maybe we could talk a little bit about that but if you're using Microsoft stuff VC package manager can do the same thing you can go grab libraries that you need so this is really you know it's something you need to know as a developer no matter what you're making because you're gonna need Network libraries you're gonna need windowing libraries you're gonna need graphics libraries and things like that so anything you might need you can get through these or you can not use these at all and do it your own way so that way you don't need to include a package manager and worry about it you can just do it your own way and actually that's that's what I've come to have used VC package manager a little bit it's actually pretty nice but I just don't like having additional dependencies basically because now if you want to work on the project you've also gotta mess with the package manager and that can be avoided and you can just use git now how you want to include your static and dynamic libraries is ultimately up to you and there is no proper standard you go to 20 different places you're not gonna get 20 different answers you'll probably get like five different answers of people doing it different ways and I want to talk a little bit about kind of my favorite kind of my sound like the sure no keep saying kind of I want to talk about the way I've recently been doing library and this is a windows thing of ever all I guess if I were on Linux I would probably do it about the same it would work about it could work about the same except you just have to make a C make file or a make file for it let me launch Visual Studio here and I want to talk a little bit about how I like to do libraries I'm just gonna do a brand new project that is empty well empty yeah do an empty project just an empty project here so it's just a new project and I'll just call it including libraries I don't deal so you can see there we go okay the new version I think I'm actually ignore that for now since I'm doing a video I'm just gonna set this up a little bit okay I'm gonna click on the project I'm gonna click this show all files so it shows me an actual structure of what's going on and of course there's nothing because I chose empty projects so what I'm gonna do isn't right-click on this project and go to add new item we're gonna do a CPP file so it's gonna be an object file something to build and we're gonna make our main so we're gonna call it main dot CPP and we'll just say and main and return 0 of course or it'll be improper we'll say exit success which is just 0 and to include that I've got to include the C standard why there is that's a that's some terrible formatting right there alright so there we go we'll just make it a little more proper real quick okay so there's our main so if we build this we build an any configuration it'll run fine it's not gonna do anything except return a 0 and yeah there goes unable to start operator 2 not completes successfully because the file contains a virus or potentially unwanted software this is a really weird error to get I have never seen this error actually operation did not complete successfully because the file contains a virus or potentially unwanted software WTF there are so-called false positives that occur to false injuries in the malware database this is often a false positive wait what thing is this and I'm so confused 86'd bug yeah it's x86 debug that doesn't that throws the error that's really strange we're gonna ignore that okay so now let's talk about including libraries let's say let's pick a common library and just use it as a sample and now I'm talking about open source libraries here there's a lot of open source libraries that you can use and if you want to only use open source you don't need a package manager you can just independently build them on your own so let's say I want to and say I want to use GL fw3 which is a win OpenGL windowing system OpenGL Vulcan context windowing system which is pretty commonly used with C++ if you're doing graphics because it works on both Windows Linux and Mac all three boast I said both cuz I was going to say Windows and Linux but it will throw a Mac in there too it's it's a survivor so GL FW how do you include it you could do it through Conan you can do it through VC package manager you see package manager might actually be the easiest just because we're ever using Visual Studio but what if you don't even wanna mess with that you just want to build the latest version of GL left over yourself and then and use that library because it's open source you should be able to do that right well you can let's go ahead and go to jail off W you can see it down or download the latest version that they released or you can see a cloned so we're gonna actually click well let's go we're for example let's click on the download first and see what does it actually download the zip of it okay whatever that's fine so you can use this file to do it I'm not gonna do that right now though I'm gonna use do a clone that way I get the latest version with my project all the time so we're gonna hit this clone button and copy the link now you can clone with visual studios get I'll have that set up right now so I'm gonna launch program called fork actually forget fork let's use git bash keep bashes even cooler okay I think my repos are actually on a different Drive so I'm Scott I'm gonna go to my key Drive I'm gonna go to well let me see what I got here I think I have in storage yeah CD storage is where I keep my projects so you basically I'm just basically going to where I keep my projects there's a bunch of stuff in storage oh wow okay well I'm gonna go to run burritos I'm pretty sure that's in here now it's under source Jesus I've got a lot of files on this on this hard drive source repos there we go and then there we go so I've got a bunch of other projects in here already I have glue but I don't have GL @ w3 so let's go ahead and clone glf w3 we'll just do a git clone and I should have that link still copied so I'm going all right click and do a paste oh it's glue my blue copy no copy so you basically just want this link of course you're off to set up a kit I assume you already did that and we're just gonna clone it and we're gonna clone it into the folder we're in right now and let it do its thing so now we can change directory into that folder and - jail - well let's just look at it yeah there it is GLW and of course if you go into gfw you will see that it has all its files and a/c make build stuff so what we want to actually do next is just launch C make C make GUI is what I'm using here on Windows and we're just gonna go ahead and build it so we're gonna go same folder except this time we're gonna go to GL w and then that's fine that's where the source code is basically the routes he make file then we want to decide where to build the binary's there's already kind of got to make some choices about where you want to be doing your includes and stuff so I'm just actually gonna do it on the root of this repo which usually people make a build folder but I have my reasons for doing it on the root on this particular project so we're gonna source code and build in the same spot in this case like I said usually people do a build folder here but I'm just not doing that purposely because really it's because of the way gfw has to see make files and makes they include a little weird if you have the build folder to me anyway but it you could do it that way it's fine you'll just have to make some adjustments later so there it goes it's doing its thing didn't find oxygen didn't find some stuff but it all configured and didn't have any errors so we're gonna open the project with Visual Studio because that's what we generated for and now we want to build for the version of our other project so we see here we've got debug 64 if we want to build that one we'll just go to build build solution and it's gonna build whenever the project it's on which is an all build currently so that's probably gonna generate all the libraries actually look at this output yeah it's still going 32 succeeded so bill no just bill all the debug 64 stuff so if we also maybe we want release 64 as well so we'll go ahead and build that one we're only gonna build the 64 bit ones but as you can see here you can also build the 32 bit ones if you want to build your other project 32 there's libraries do have to match debug release 32 64 so I built the debug and the release of 64 that's fine I could also do men's size release but I'm just gonna stick with the release okay so we're actually done here I'm just gonna close out all this now close out all this close this and we've got build we do an LS here we'll see a bunch of new files all the visual studio stuff and just once again I'll say it's not normal to build everything into the same repo at least from a/c make standard but I'm just doing it anyway because it's gonna make things easier so now let's say we just wanted we just want to do some GL fw when doing actually you know let's let's look at the samples most code open source code comes with some samples so we're gonna do a file open project or solution and what we were just gonna open GL fw the solution here open here we are so we'll see some tests some examples so let's just pick one of these examples and more throw it into our code cuz it's gonna be essentially the same I'm gonna change this into all files so I can actually see their structure I've got a lot of things going on here and they have these little minus signs on them which means they're not included in the project this is called Boyne so well let's just go to this let's just run it let's go to set a start a project at least 64 and while I play there we go you got a little classic amiga demo of a boy yeah let's just do it we'll just go to it let's go to this open the files let's open our other project what was it called just live including lives yeah there we go we don't really have anything in this yet but I just want to show you how to include y'all w3 so they've got a lot of code here this is kind of a lot to unpack and work with here so let's see what they got going on a lot of different stuff building here oh this is all the projects that's that's what's going on here there's like all those sample projects in the same folder so that's why it's so confusing I think the only thing we really need is this main one here the Boeing and it should have a main so let's search for a main and see if they got yeah they got it down here so there's the main so basically what they do is they declare a GL FW window I try to initialize GLW make sure it doesn't fail and then they create the window and then they set up some some stuff and then they go through a main loop and and do some stuff okay so if they want it we'll just grab all this code here in the main and I mean it's gonna paste it into mine can we see a bunch of red because we haven't included GLW of course the includes gonna look like this I think it's GLW we don't even see it it doesn't see it because we have included it so now I'm gonna talk about how to include that library from the build we did from the other project all you do is you right click on your project here and go to properties and in the properties you want to change this to all config all platforms if it's not already and you basically want to set up where the headers are to JAL fw and where the libraries are XG l fw and you also need to link them so let's just go through here I'm gonna do it in the C++ and under general we have additional include libraries so I'm going to click the drop down here and I'm gonna say okay since we know GL fw is in the same folder resource slash repos we can go back a directory from our build and back a directory from our project and then we go into G lfw which is I think all lowercase and then from here we should go into includes I believe and that should do it so later okay and apply and if I got that right let me just see if I got it right let's go into GL w it's include not includes okay so we're gonna change that so I'm gonna go to edit change it to just include of course has to match exactly one won't find it and there we go so now we've got that I'm just gonna hit OK here of course is more we got to do but now it should be able to find this so we should be able to do include so you'll start typing in you know we see it there Visual Studio finds it because it's in its include path so there we got the jail if W stuff exit failure of course comes from standard the C standard so will include that of course we could just include like we could just put negative owners on there but we'll stick with their example and they've got some functions here for their callbacks that were probably declared in the other file so what they've got here is they do the whole window creation they set some callbacks I'm gonna go ahead and comment out all these callbacks and yeah I'm just everything here everything about the window and there's the for loop and we will comment out the stuff that's not working as you can see the swap buffers and the poll events that's fine it's not gonna do anything right now but at least go through this loop and oh wait if the window closes and that breaks usually people do while window they usually put this as the wild clause with a knot in front of it but this works you it was essentially the same so and at the end they they terminate which destroys the window for gfw so now if we run this we should get the GL fw popping up right right well we haven't linked the library yet we've we've got to the header and so it can see this code but when we go to build it it's not gonna have the object files that it needs and we're gonna get this linker error so that's the other part of this is a linker so we go back into the properties just right-click on the include libraries go to properties and this time we're gonna go down a little further to the word says linker and under linker can expand it out there's general it's nothing really to change here for us but input is where we're gonna look at we're gonna look at input pretty carefully here actually I think we got to go up to although there's one more thing I think it's under this VCC directories yeah there's include libraries or library directories yeah that one's under this VCC so under the input here we're gonna put the live under the dependencies but we also got to put the path to it in this VCC directories so which we do first doesn't matter I know that the opengl libe well let's just take a look at it so we'll go back to this folder structure where we built and/or the lives go now this is tricky sometimes because it goes in a different place depending on people how people set up their see make I'm pretty sure on this it goes into source and then there should be little some lives there's a debug and a release so let's look at yeah debug has one since I built a debug one police has one since I built the release and then there's also an x64 here which has a debug and release but these are not what you think they are just like logs so they're not the right one now it's kind of tricky about this is it doesn't differentiate the 32 and 64 I guess well we only built this 64 once in lis but if we open this project and we build with 32 ones they're supposed to go to a different directory I don't think GL FWC make is setup like that which kind of throws me off a little bit cuz usually it'll go into a really like an x64 or an x86 and then a release and debug sort of like this except in a build folder yeah but in this case the build folder is the source goes on the source and it just has a debug and release but they don't say what they are however we'll get an error if they're wrong anyway but we do gonna get the debug and release correct so since this is all configs all platforms we can use some macros for that path in later but let's go ahead and add the glf w dot live because we saw that's what it was Sheila W dot live I mean let me look again Jill fw3 dot line and this one is also do you have Toby 3 dot live so we're just got to get that exact live name and hit OK hit apply and now we can go build again we'll get the same error except might be a little different might say can't find it yeah there it is cannot open file and it's because it needs the path to this it doesn't magically know to scan your whole computer for the live or something you got a it would take forever every build if it did that so we're going to go back into properties here and go to the VCC directories look at the library directories see the includes we've already got down here it's kind of redundant but yeah we've already got the include set up right there the libraries however it says different options here that's because we're set to all platforms some of these platforms are different you can see there's reference path 460 for reference perhaps 486 so when you go to all platforms it just says hey there's different options but we can still edit it let me make sure I'm well there we go library I was I was talking about reverence there but it's the same with the libraries alright so we'll go to edit these libraries and this time we're gonna add the path once again to gfw so we know we go back a directory from our build folder and we go back a directory from our project and then we can go into GL fw 3 and then we saw that it was in source and now this next part it could be another deep and either debug or lise so what we do here is we put a macro for and the macro is just a dollar sign and then in parentheses configuration I hope I'm saying that I'm hoping doing this rate so this will path next to either whatever our configuration is which is debug or release so this is either the word debug or release or if we had a midsize release it could also go to that but for right now we're only working with debug and release and that's what this configuration turns into so we're gonna hit OK we're gonna hit apply I don't think we need the last slash there I think that's actually going to bug it out because I'm pretty sure the slash is included all right so there we go and that's just where GL FW was that's that's all it is so if Jill fw ever gets rebuilt it'll still path to the same place where we're building it now if you built your gfw somewhere else like into the end of the build it's gonna probably be GLW build source debug all right so according to our check-in it could be different check for yourself all right so now when we build this it should succeed unless we've messed something up ago it built and there we go we got the window and we got there title because we copypasta that in there we go so that's how you include libraries from open source stuff that's up on github and as a see make is is just like that so you could do this for every every one of them and anytime gow3 master gets updated you could pull those updates and rebuild and get the newest latest features right away and this is basically how I do libraries these days is I bring in the source in custom lincom if it's something that's close source and then yeah I might go download the binaries and when and in case you don't know the binary is this live so you can get them pre-built for 64 32 release debug it's the same thing it's just somebody already went through and built it and then they just uploaded the libe for you to link and of course you need the headers to so that you can do this whole thing okay well I think that explains it enough I'm going to do a part two of this where maybe I add in some OpenGL stuff if you want if you guys want let me know below if you want to see a part two if you want me to continue with this I could talk more about other types of limes I could talk about dynamic lives I could add our other libraries like I could start adding anything else any other lives to this or I could show you how to build your own lives and link them in just with visual studio because you can do it you can just right click here and go to add a new project and when you pick a project here there are static libraries which is what GL fw3 is there there's also dynamic libraries which you can link in so that way you can have a project have a bunch of different things going on some of them are just libraries you don't run them you just build them every once in a while so that your main project your main actual executable will use those and there you go so there's a lot to it I think I've covered a pretty good chunk of libraries I hope that helps someone out there that's trying to understand libraries more and is maybe stuck it is essentially the same on Linux I haven't played around libraries as much on Linux but it's essentially the same thing you just gotta you gotta look up the commands to put in your make file or C make actually I think see Mike handles it for you even on Linux but if you're making your own make file then you got to look up the commands okay well peace out you guys I'll see you in the next episode Mac from Co tech materials out stay safe out there and keep code [Music]
Info
Channel: Code, Tech, and Tutorials
Views: 12,408
Rating: 4.6480937 out of 5
Keywords: coding, programming, c++, learning, howto, what is coding, coding for beginners, libraries in c++, understanding libraries in c++, static vs dynamic
Id: xBfwQv8mxCI
Channel Id: undefined
Length: 30min 22sec (1822 seconds)
Published: Thu Jun 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.