How to CMake Good - 1b - Adding a Library

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody and welcome to how to seem a good part 1b last time we made an executable this time we're gonna make a library some of the most common things you'll do in C make of course are making executables and libraries so be good to show making libraries right after making executables at this project open here that I had from left from last time he creates a simple executable called Sima good in a main dot cpp that prints hello world i'm going to say i want to take this functionality of printing hello world and i want to move it into a library so i'll implement the files for that library now so fast-forward through it so you don't have to watch look at these hackers fields okay so I've created my library files now to use that library I would first include oh hello dot HPP and I would change my call to call the function that I defined I'll say hello that's a simple function that it all it says is hello world to standard out just like our main used to do I've deleted the project configuration because we're going to go through that again let me go through the same steps as last time I'll make der bilt CD build C make dot dot and let the configuration run you can figure it successfully but if I type make I get a linker error because main dot CPP has an undefined reference to hello say hello that's because the linker doesn't know where the definition of this function is because we haven't told it a very simple way to work around this is to add the source file that contains the function to our executable target and then if I run CMake again and I run make it builds and it executes but that's not the point of the exercise so I'm going to take that file out I'm going to declare a new library I'll call it say hello I'll add the two source files hello HPP and hello cpp and now if i run see me again and then i run make we see it compiled hello dot cpp like we wanted it says it linked the executable but that failed and then it also says it linked a static library called lips a hello dot a and that's this library that we declare declared here but we're still getting this linker error because we haven't told c make to link this library into this executable and that's as simple as adding a single c make function invocation called target linked libraries first argument will be the library or executable we're linking into in this case the execute will seem a good the next argument will be the link interface mode I'm going to use private for now we'll talk about those modes later they're very important the library I'm linking into the executable called say hello and that is all we need to do now I've been running C make manually from the command line every time I change the C make lists but I don't actually need to do that see McCain rates build systems that will automatically detect if they've been made out of date so if I run make it'll run C make for me see there's the output from C make configure and then the regular build runs you'll also notice that the linker error went away and now if we run the program it runs exactly as if we included the hello dot cpp in the executable note here in the add library call that we did not specify a type of library but it did build a type it built a static library and it even says static library right here the default for add library is to build static libraries we can specify the library type after the library name in the add library call if I specify shared and then I run make again the applet says it create linked AC XX shared library lips say hello s oh and our program still runs exactly as it did if I run LD d on our executable it'll print out the linker dependency of Lib say hello so pointing to the Lib hello so that we built from C make and it's in the build path and we can see the file right over here in the built directory if we change the shared type back to static manually saying static and we run the build it linked a static library Lopez say hello a exactly like it did when we didn't specify a library type and if we look at the executable the dynamic linker dependency on lips say hello dot s o is gone there's a third type of library called module library if I run CMake again we're gonna see an error target linked libraries doesn't work the same on mantra libraries again if we remove any library type and read seat and run C make again we get a static library we can see that it didn't rebuild anything because I changed it from sank static to saying nothing and the default is already static so it didn't need to rerun anything a lot of people don't know this and they'll try to implement this the feature themselves but C make is able to change the default of what add library will generate if I say - D build shared libs equals true and then I run CMake again then all run make and here it's generated ellipse say hello dot s oh we still haven't specified a library type but C make decided since the build shared libs is true that add libraries default should instead be shared libraries this functionality is already built into C make so you don't have to do anything special to get it that's all I wanted to talk about in this video check the video description for any addenda errata important links or more information in the next video we'll talk about setting include directories and creating a more complex or structure until then keep see making good
Info
Channel: vector-of-bool
Views: 78,644
Rating: 4.9049358 out of 5
Keywords: cmake, tutorial, library, software, programming
Id: abuCXC3t6eQ
Channel Id: undefined
Length: 6min 44sec (404 seconds)
Published: Sun Aug 12 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.