title
How the C++ Linker Works

description
Twitter ► https://twitter.com/thecherno Instagram ► https://instagram.com/thecherno Patreon ► https://patreon.com/thecherno Series Playlist ► https://www.youtube.com/playlist?list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb How the C++ Compiler Works ► https://youtu.be/3tIqpEmWMLI BEST laptop for programming! ► http://geni.us/pakTES My FAVOURITE keyboard for programming! ► http://geni.us/zNhB FAVOURITE monitors for programming! ► http://geni.us/Ig6KBq MAIN Camera ► http://geni.us/CYUQ MAIN Lens ► http://geni.us/ZM3CmG Microphone ► http://geni.us/wqO6g7K Slack ► https://slack.thecherno.com Stream ► http://www.twitch.tv/thecherno Website ► http://www.thecherno.com Facebook ► http://www.facebook.com/thecherno

detail
{'title': 'How the C++ Linker Works', 'heatmap': [{'end': 508.868, 'start': 428.3, 'weight': 0.779}, {'end': 735.854, 'start': 693.424, 'weight': 0.787}, {'end': 838.289, 'start': 766.678, 'weight': 0.84}, {'end': 886.283, 'start': 863.2, 'weight': 0.768}], 'summary': 'Covers c++ linking and errors, debugging and testing functions, and solutions for linking errors, emphasizing visual studio project demonstration, managing log functions, and practical techniques for successful project linking.', 'chapters': [{'end': 234.922, 'segs': [{'end': 67.225, 'src': 'embed', 'start': 39.614, 'weight': 0, 'content': [{'end': 45.537, 'text': 'So, if we decide to split our program across multiple C++ files, which is, of course, very common,', 'start': 39.614, 'duration': 5.923}, {'end': 49.658, 'text': 'we need a way to actually link those files together into one program.', 'start': 45.537, 'duration': 4.121}, {'end': 51.899, 'text': 'And that is the primary purpose of what the linker does.', 'start': 49.859, 'duration': 2.04}, {'end': 57.082, 'text': "Even if you don't have functions in external files, like, for example, you've written your entire program in one file.", 'start': 52.18, 'duration': 4.902}, {'end': 60.543, 'text': 'The application still needs to know where the entry point is.', 'start': 57.682, 'duration': 2.861}, {'end': 62.424, 'text': 'So in other words, where the main function is.', 'start': 60.563, 'duration': 1.861}, {'end': 67.225, 'text': "So that when you actually run your application, the C runtime library can say, hey, here's the main function.", 'start': 62.724, 'duration': 4.501}], 'summary': 'Linker in c++ combines multiple files into one program, including locating the main function.', 'duration': 27.611, 'max_score': 39.614, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/H4s55GgAg0I/pics/H4s55GgAg0I39614.jpg'}, {'end': 98.657, 'src': 'embed', 'start': 71.967, 'weight': 1, 'content': [{'end': 75.648, 'text': "So it still needs to link the main function and everything like that, even if you don't have all the files.", 'start': 71.967, 'duration': 3.681}, {'end': 78.009, 'text': 'The best way to explain this is by showing some examples.', 'start': 75.828, 'duration': 2.181}, {'end': 79.209, 'text': "So let's jump over and take a look.", 'start': 78.049, 'duration': 1.16}, {'end': 87.472, 'text': "So here in Visual Studio we've got a very simple project that just contains one source file math.cpp and inside there we have two functions log and multiply.", 'start': 79.429, 'duration': 8.043}, {'end': 93.995, 'text': 'The multiply function actually calls the log function, prints out the word multiply to the console, and then returns a times b.', 'start': 87.492, 'duration': 6.503}, {'end': 98.657, 'text': "Pretty simple stuff, however this isn't an actual application since of course it doesn't contain a main function.", 'start': 93.995, 'duration': 4.662}], 'summary': 'Visual studio project lacks main function, contains math.cpp with log and multiply functions.', 'duration': 26.69, 'max_score': 71.967, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/H4s55GgAg0I/pics/H4s55GgAg0I71967.jpg'}, {'end': 132.825, 'src': 'embed', 'start': 105.086, 'weight': 2, 'content': [{'end': 108.311, 'text': "And there's actually a way that you can differentiate between the two in Visual Studio.", 'start': 105.086, 'duration': 3.225}, {'end': 113.158, 'text': 'If you press Control F7 or if you press the compile button, only compilation will happen.', 'start': 108.411, 'duration': 4.747}, {'end': 114.96, 'text': 'No linking will ever happen.', 'start': 113.458, 'duration': 1.502}, {'end': 121.883, 'text': 'However, if you build your project or if you hit F5 to run your project, it will actually compile and then link.', 'start': 115.521, 'duration': 6.362}, {'end': 126.124, 'text': "So if I just hit control F7, you'll see that I actually get no errors.", 'start': 122.203, 'duration': 3.921}, {'end': 129.283, 'text': "Everything's fine because the compilation was successful.", 'start': 126.204, 'duration': 3.079}, {'end': 132.825, 'text': "It generated that math.obj file, the object file, and everything's great.", 'start': 129.365, 'duration': 3.46}], 'summary': 'In visual studio, pressing control f7 triggers compilation only, without linking. building or hitting f5 will compile and then link the project.', 'duration': 27.739, 'max_score': 105.086, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/H4s55GgAg0I/pics/H4s55GgAg0I105086.jpg'}, {'end': 211.488, 'src': 'embed', 'start': 181.666, 'weight': 3, 'content': [{'end': 184.908, 'text': 'And it even tells us over here that this happened during the link stage.', 'start': 181.666, 'duration': 3.242}, {'end': 191.914, 'text': "It's really important that you know what kind of error you get, whether it's a compiling error or a linking error, because of course,", 'start': 185.209, 'duration': 6.705}, {'end': 193.696, 'text': 'you need to know that so that you can fix it properly.', 'start': 191.914, 'duration': 1.782}, {'end': 197.539, 'text': 'So So in this case, we get an error which tells us the entry point must be defined.', 'start': 193.836, 'duration': 3.703}, {'end': 200.641, 'text': 'Again, that is because we are compiling this as an application.', 'start': 197.979, 'duration': 2.662}, {'end': 205.784, 'text': 'If we go to our properties and we take a look at what configuration type we have set here,', 'start': 200.721, 'duration': 5.063}, {'end': 211.488, 'text': "you'll see it is set to application.exe and every exe file has to have some kind of entry point.", 'start': 205.784, 'duration': 5.704}], 'summary': 'Understanding error type is crucial; entry point needed for application.exe', 'duration': 29.822, 'max_score': 181.666, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/H4s55GgAg0I/pics/H4s55GgAg0I181666.jpg'}], 'start': 0.109, 'title': 'C++ linking and errors', 'summary': 'Covers the purpose of c++ linking, symbol and function linking process, and visual studio project demonstration. it also explains compilation and linking errors in visual studio, highlighting differentiation, triggering methods, and the importance of error identification.', 'chapters': [{'end': 104.706, 'start': 0.109, 'title': 'Understanding c++ linking process', 'summary': 'Explains the purpose of c++ linking, which involves finding and linking symbols and functions from separate source files into one program, even if all functions are in one file, and demonstrates a simple project in visual studio.', 'duration': 104.597, 'highlights': ["The C++ linker's primary focus is to find and link symbols and functions from separate object files into one program, even if all functions are in one file.", 'Even if the entire program is written in one file, the linker still needs to know the entry point, i.e., the main function, to start executing the application.', 'The chapter demonstrates a simple project in Visual Studio containing one source file with two functions, log and multiply, and explains the two stages of compilation: compiling and linking.']}, {'end': 234.922, 'start': 105.086, 'title': 'Compilation and linking errors in visual studio', 'summary': 'Explains the differentiation between compilation and linking errors in visual studio, highlighting that hitting control f7 or the compile button triggers only compilation, while building the project or hitting f5 compiles and links the code. it also emphasizes the importance of identifying whether an error is a compilation or linking error, providing examples of error codes and their association with each stage.', 'duration': 129.836, 'highlights': ['The chapter highlights the differentiation between compilation and linking errors in Visual Studio, emphasizing that hitting Control F7 or the compile button triggers only compilation, while building the project or hitting F5 compiles and links the code.', 'It explains the significance of identifying whether an error is a compilation or linking error by providing examples of error codes and their association with each stage.', "It emphasizes the need to know the type of error to fix it properly, providing the example of an 'entry point must be defined' error that occurs due to compiling an application without a specified entry point.", "The chapter also mentions the ability to specify a custom entry point in Visual Studio, highlighting that it doesn't necessarily have to be the main function."]}], 'duration': 234.813, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/H4s55GgAg0I/pics/H4s55GgAg0I109.jpg', 'highlights': ["The C++ linker's primary focus is to find and link symbols and functions from separate object files into one program, even if all functions are in one file.", 'The chapter demonstrates a simple project in Visual Studio containing one source file with two functions, log and multiply, and explains the two stages of compilation: compiling and linking.', 'The chapter highlights the differentiation between compilation and linking errors in Visual Studio, emphasizing that hitting Control F7 or the compile button triggers only compilation, while building the project or hitting F5 compiles and links the code.', "It emphasizes the need to know the type of error to fix it properly, providing the example of an 'entry point must be defined' error that occurs due to compiling an application without a specified entry point."]}, {'end': 582.955, 'segs': [{'end': 302.995, 'src': 'embed', 'start': 234.922, 'weight': 0, 'content': [{'end': 242.066, 'text': "so if we back out of here and actually write that main function, i'll just write int main and then i'll build my project again.", 'start': 234.922, 'duration': 7.144}, {'end': 248.627, 'text': "you'll see that we no longer get that linking error and that we were successfully able to generate that EXC file.", 'start': 243.422, 'duration': 5.205}, {'end': 255.613, 'text': "All right, so now that we've established that, let's go ahead and print out the value of that multiply function.", 'start': 248.727, 'duration': 6.886}, {'end': 258.276, 'text': "So we'll multiply five and eight together.", 'start': 255.653, 'duration': 2.623}, {'end': 266.05, 'text': 'So what we should see is this message being logged and then the value 40 being printed.', 'start': 261.348, 'duration': 4.702}, {'end': 270.352, 'text': "Let's also add a cin.get so that our console doesn't close immediately.", 'start': 266.19, 'duration': 4.162}, {'end': 273.513, 'text': "Then I'll just click on this local Windows debugger button to run this.", 'start': 271.072, 'duration': 2.441}, {'end': 276.454, 'text': 'As you can see, we get multiply and 40.', 'start': 274.593, 'duration': 1.861}, {'end': 278.335, 'text': 'So our application seems to be running correctly.', 'start': 276.454, 'duration': 1.881}, {'end': 281.537, 'text': 'Great Now, suppose I had these in multiple files.', 'start': 278.615, 'duration': 2.922}, {'end': 287.462, 'text': "For example, this log doesn't really need to be in this math file because of course this just logs the message.", 'start': 281.597, 'duration': 5.865}, {'end': 291.366, 'text': "So why don't I have a separate file that actually contains all of my logging related functions?", 'start': 287.542, 'duration': 3.824}, {'end': 298.392, 'text': "I'm going to right click on source files and add a new C++ file called log.cpp, and then I'll click add.", 'start': 291.546, 'duration': 6.846}, {'end': 302.995, 'text': "I'm going to grab that log function from here and move it into my log.cpp file.", 'start': 298.692, 'duration': 4.303}], 'summary': 'Successfully resolved linking error and ran application with multiply function outputting 40.', 'duration': 68.073, 'max_score': 234.922, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/H4s55GgAg0I/pics/H4s55GgAg0I234922.jpg'}, {'end': 350.346, 'src': 'embed', 'start': 321.826, 'weight': 6, 'content': [{'end': 325.568, 'text': "and we'll add this so that we have a declaration of the log function in this math.cpp file.", 'start': 321.826, 'duration': 3.742}, {'end': 328.69, 'text': "I'll hit Ctrl F7 and you can see that compiling worked.", 'start': 326.148, 'duration': 2.542}, {'end': 330.772, 'text': "Now let's go ahead and build our entire project.", 'start': 329.071, 'duration': 1.701}, {'end': 338.939, 'text': 'We get several errors here, compile errors, telling us that cout is not found because we need to actually include iostream.', 'start': 331.152, 'duration': 7.787}, {'end': 342.502, 'text': "Once we've done that, let's build our entire project.", 'start': 340.3, 'duration': 2.202}, {'end': 343.442, 'text': 'All right, great.', 'start': 342.942, 'duration': 0.5}, {'end': 344.683, 'text': 'It seemed to work successfully.', 'start': 343.542, 'duration': 1.141}, {'end': 347.805, 'text': "Now let's take a look at one type of linking error that we might get.", 'start': 344.923, 'duration': 2.882}, {'end': 350.346, 'text': "This one's called unresolved external symbol.", 'start': 347.985, 'duration': 2.361}], 'summary': 'Compiling math.cpp with log function, resolving errors, successful build', 'duration': 28.52, 'max_score': 321.826, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/H4s55GgAg0I/pics/H4s55GgAg0I321826.jpg'}, {'end': 403.182, 'src': 'embed', 'start': 378.125, 'weight': 3, 'content': [{'end': 385.111, 'text': "It believes that there is a log function somewhere, but it's going to be the job of the linking stage to actually find that log function.", 'start': 378.125, 'duration': 6.986}, {'end': 389.193, 'text': "So if I build my entire project now, you'll see we actually get an error.", 'start': 385.751, 'duration': 3.442}, {'end': 395.377, 'text': 'This is a linking error because you can see that it begins with the LNK letters and the error says unresolved external symbol.', 'start': 389.413, 'duration': 5.964}, {'end': 397.698, 'text': 'Now it tells us exactly what symbol is missing.', 'start': 395.577, 'duration': 2.121}, {'end': 398.839, 'text': "It's that log function.", 'start': 397.959, 'duration': 0.88}, {'end': 403.182, 'text': "It even tells us where we reference it, that we're referencing it in a function called multiply.", 'start': 399.059, 'duration': 4.123}], 'summary': 'Linking stage needed to find log function, causes lnk error in multiply function.', 'duration': 25.057, 'max_score': 378.125, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/H4s55GgAg0I/pics/H4s55GgAg0I378125.jpg'}, {'end': 508.868, 'src': 'heatmap', 'start': 428.3, 'weight': 0.779, 'content': [{'end': 434.304, 'text': "So the linker doesn't have to link this function call to actually call the log function because we never called the log function.", 'start': 428.3, 'duration': 6.004}, {'end': 442.029, 'text': 'Another interesting note is that if I do call the log function here and multiply, however, I comment this line out so that I never call multiply,', 'start': 434.684, 'duration': 7.345}, {'end': 444.011, 'text': 'which in turn never calls log.', 'start': 442.029, 'duration': 1.982}, {'end': 449.074, 'text': "If I build my project now, you'll see that I still get a linking error and you might be like what?", 'start': 444.331, 'duration': 4.743}, {'end': 450.315, 'text': 'But why is that happening?', 'start': 449.154, 'duration': 1.161}, {'end': 452.737, 'text': "I'm not calling multiply anywhere.", 'start': 450.495, 'duration': 2.242}, {'end': 455.138, 'text': 'Why is it complaining about a linking error??', 'start': 453.317, 'duration': 1.821}, {'end': 460.122, 'text': "Shouldn't it have just removed the function entirely, since it's essentially dead code that's never used? Wrong.", 'start': 455.258, 'duration': 4.864}, {'end': 467.266, 'text': "Because whilst we're not using the multiply function in this file, we actually could technically use it in another file.", 'start': 460.582, 'duration': 6.684}, {'end': 469.728, 'text': 'And so the linker does actually need to link that.', 'start': 467.546, 'duration': 2.182}, {'end': 472.109, 'text': 'If we could somehow tell the compiler that hey,', 'start': 470.008, 'duration': 2.101}, {'end': 479.074, 'text': "this function multiply I'm only ever going to use it inside this file then of course we could remove that linking necessity,", 'start': 472.109, 'duration': 6.965}, {'end': 481.996, 'text': 'since if multiply is never called, it never needs to call log.', 'start': 479.074, 'duration': 2.922}, {'end': 484.057, 'text': 'Oh wait, there is a way we could do that.', 'start': 482.316, 'duration': 1.741}, {'end': 487.619, 'text': 'If we come over here and write the word static in front of multiply,', 'start': 484.377, 'duration': 3.242}, {'end': 495.062, 'text': 'that basically means that this multiply function is only declared for this translation unit, which is this math.cpp file in our case.', 'start': 487.619, 'duration': 7.443}, {'end': 501.345, 'text': "And since multiply is never called inside this file, if I build, we won't get any linking errors.", 'start': 495.402, 'duration': 5.943}, {'end': 505.707, 'text': 'If I bring back my comment though, and then I build, of course we will get a linking error.', 'start': 501.725, 'duration': 3.982}, {'end': 508.868, 'text': 'In this case, we actually ended up modifying the name of the function.', 'start': 506.067, 'duration': 2.801}], 'summary': 'Explaining the impact of function calls and the necessity of linking in a programming context.', 'duration': 80.568, 'max_score': 428.3, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/H4s55GgAg0I/pics/H4s55GgAg0I428300.jpg'}, {'end': 511.609, 'src': 'embed', 'start': 487.619, 'weight': 4, 'content': [{'end': 495.062, 'text': 'that basically means that this multiply function is only declared for this translation unit, which is this math.cpp file in our case.', 'start': 487.619, 'duration': 7.443}, {'end': 501.345, 'text': "And since multiply is never called inside this file, if I build, we won't get any linking errors.", 'start': 495.402, 'duration': 5.943}, {'end': 505.707, 'text': 'If I bring back my comment though, and then I build, of course we will get a linking error.', 'start': 501.725, 'duration': 3.982}, {'end': 508.868, 'text': 'In this case, we actually ended up modifying the name of the function.', 'start': 506.067, 'duration': 2.801}, {'end': 511.609, 'text': "However, it's not just the name of the function that matters.", 'start': 508.928, 'duration': 2.681}], 'summary': 'The multiply function is only declared in math.cpp and not called, resulting in no linking errors when built.', 'duration': 23.99, 'max_score': 487.619, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/H4s55GgAg0I/pics/H4s55GgAg0I487619.jpg'}], 'start': 234.922, 'title': 'Debugging, testing & log functions in c++', 'summary': "Covers debugging and testing a function in c++, generating an exc file, and verifying the correct output. it also discusses managing log functions, including separation, resolving errors, and using the 'static' keyword, highlighting the importance of matching function signatures to avoid linking errors.", 'chapters': [{'end': 281.537, 'start': 234.922, 'title': 'Debugging and function testing', 'summary': 'Demonstrates debugging and testing a function in c++, successfully generating an exc file and verifying the correct output of a multiplication function.', 'duration': 46.615, 'highlights': ['Successfully generating the EXC file after writing the main function and building the project, eliminating the linking error.', 'Verifying the correct output of the multiply function by multiplying 5 and 8, obtaining the value 40 as expected.', "Demonstrating the application running correctly by logging the message 'multiply' and the value 40, ensuring proper functionality."]}, {'end': 582.955, 'start': 281.597, 'title': 'Managing log functions in c++', 'summary': "Discusses the management of log functions in c++ code, covering the separation of logging functions into a separate file, resolving compile and linking errors, and the use of 'static' keyword to limit function scope, with an emphasis on the importance of matching function signatures to avoid linking errors.", 'duration': 301.358, 'highlights': ['Separating logging functions into a separate file called log.cpp to organize code and improve maintainability.', 'Resolving compile errors by adding necessary includes like iostream and declaring the log function in the math.cpp file to ensure successful compilation.', 'Understanding and addressing linking errors by ensuring matching function signatures, including return type and parameters, to avoid unresolved external symbol errors.', "Utilizing the 'static' keyword to limit the scope of a function to a specific translation unit in order to eliminate linking errors for unused functions."]}], 'duration': 348.033, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/H4s55GgAg0I/pics/H4s55GgAg0I234922.jpg', 'highlights': ['Successfully generating the EXC file after writing the main function and building the project, eliminating the linking error.', "Demonstrating the application running correctly by logging the message 'multiply' and the value 40, ensuring proper functionality.", 'Verifying the correct output of the multiply function by multiplying 5 and 8, obtaining the value 40 as expected.', 'Understanding and addressing linking errors by ensuring matching function signatures, including return type and parameters, to avoid unresolved external symbol errors.', "Utilizing the 'static' keyword to limit the scope of a function to a specific translation unit in order to eliminate linking errors for unused functions.", 'Separating logging functions into a separate file called log.cpp to organize code and improve maintainability.', 'Resolving compile errors by adding necessary includes like iostream and declaring the log function in the math.cpp file to ensure successful compilation.']}, {'end': 935.783, 'segs': [{'end': 609.219, 'src': 'embed', 'start': 582.975, 'weight': 0, 'content': [{'end': 587.06, 'text': "So the other type of linking error that's pretty common is when we have duplicate symbols.", 'start': 582.975, 'duration': 4.085}, {'end': 592.968, 'text': 'So in other words, we have functions or variables which have the same name and the same signature.', 'start': 587.521, 'duration': 5.447}, {'end': 597.711, 'text': 'So two identically named functions which have the same return value and the same parameters.', 'start': 593.268, 'duration': 4.443}, {'end': 599.653, 'text': "If that happens, we're in trouble.", 'start': 597.991, 'duration': 1.662}, {'end': 603.395, 'text': "The reason we're in trouble is because the linker doesn't know which one to link to.", 'start': 600.173, 'duration': 3.222}, {'end': 604.156, 'text': "It's ambiguous.", 'start': 603.455, 'duration': 0.701}, {'end': 609.219, 'text': 'So back in our code, if I was, for example, to write another version of this function,', 'start': 604.416, 'duration': 4.803}], 'summary': 'Common linking error: duplicate symbols cause ambiguity in the linker.', 'duration': 26.244, 'max_score': 582.975, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/H4s55GgAg0I/pics/H4s55GgAg0I582975.jpg'}, {'end': 644.674, 'src': 'embed', 'start': 618.706, 'weight': 1, 'content': [{'end': 623.227, 'text': "yeah, okay, since I'm compiling this file, I can obviously say that you've made a mistake.", 'start': 618.706, 'duration': 4.521}, {'end': 624.868, 'text': "This code just isn't valid.", 'start': 623.628, 'duration': 1.24}, {'end': 635.011, 'text': "So this is an example of having duplicate symbols where the compiler can actually save us because this all happens in one file and no linking actually needs to happen to see that we've got an error.", 'start': 625.508, 'duration': 9.503}, {'end': 640.733, 'text': "However, if I was to move this into a different file, for example, I'll move it back into our math file.", 'start': 635.131, 'duration': 5.602}, {'end': 642.653, 'text': 'right over here.', 'start': 641.793, 'duration': 0.86}, {'end': 643.914, 'text': 'so we still have our declaration.', 'start': 642.653, 'duration': 1.261}, {'end': 644.674, 'text': 'i can leave that there.', 'start': 643.914, 'duration': 0.76}], 'summary': 'Compiling code reveals duplicate symbols, error detected. moving code to different file avoided linking issues.', 'duration': 25.968, 'max_score': 618.706, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/H4s55GgAg0I/pics/H4s55GgAg0I618706.jpg'}, {'end': 735.854, 'src': 'heatmap', 'start': 693.424, 'weight': 0.787, 'content': [{'end': 696.166, 'text': "I'm going to call this file log.h and click add.", 'start': 693.424, 'duration': 2.742}, {'end': 702.45, 'text': "Now inside here, I'm going to grab this log function and make sure that I'm declaring it inside this header file.", 'start': 696.406, 'duration': 6.044}, {'end': 707.973, 'text': "If I go back to logcpp, I'll write some kind of other function, for example init log.", 'start': 702.59, 'duration': 5.383}, {'end': 712.796, 'text': "that's just going to call the log function and say that it's initialized the log.", 'start': 707.973, 'duration': 4.823}, {'end': 717.479, 'text': "Of course, if we try and compile now, we're going to get an error because we need that log function.", 'start': 713.336, 'duration': 4.143}, {'end': 719.94, 'text': "So I'll include log.", 'start': 717.939, 'duration': 2.001}, {'end': 725.664, 'text': "Back over here in math.cpp, instead of having this declaration here, I'm also going to include log.", 'start': 721.301, 'duration': 4.363}, {'end': 735.854, 'text': "So great, we're calling the log function from both the multiply function inside the math.cpp file, as well as the init log inside the log.cpp file.", 'start': 727.788, 'duration': 8.066}], 'summary': 'Creating log.h file and calling log function from multiple files.', 'duration': 42.43, 'max_score': 693.424, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/H4s55GgAg0I/pics/H4s55GgAg0I693424.jpg'}, {'end': 838.289, 'src': 'heatmap', 'start': 766.678, 'weight': 0.84, 'content': [{'end': 776.85, 'text': "So what's actually happened is it's taken this log function, popped it over here like so, into log.cpp, and then also over here.", 'start': 766.678, 'duration': 10.172}, {'end': 779.814, 'text': 'And now you can see that we of course do have two log functions.', 'start': 776.99, 'duration': 2.824}, {'end': 782.917, 'text': "So how do we fix this? Well, we've got a few options here.", 'start': 780.254, 'duration': 2.663}, {'end': 788.603, 'text': "If we undo all of this so that we're including log again, we could mark this log function as static.", 'start': 783.037, 'duration': 5.566}, {'end': 794.589, 'text': 'That means that the linking that should happen to this log function should only be internal, which means that this log function,', 'start': 788.943, 'duration': 5.646}, {'end': 800.915, 'text': 'when it gets included into log.cpp and math.cpp, is going to be just internal to this file, kind of like what we did with multiply.', 'start': 794.589, 'duration': 6.326}, {'end': 808.44, 'text': "So basically log and math will have their own versions of this function called log, and it won't be visible to any other object files.", 'start': 801.235, 'duration': 7.205}, {'end': 811.863, 'text': "So if we just compile this now, you'll see that we won't get any linking errors.", 'start': 808.701, 'duration': 3.162}, {'end': 814.525, 'text': 'Another thing that we could do to this is make it inline.', 'start': 812.183, 'duration': 2.342}, {'end': 819.809, 'text': "Of course, inline just means that it's going to take our actual function body and replace the call with it.", 'start': 814.805, 'duration': 5.004}, {'end': 825.033, 'text': 'So in this case, this log initialized log would just become that.', 'start': 819.949, 'duration': 5.084}, {'end': 830.522, 'text': "So if we were to do something like that and build, you'll see that we get no errors either.", 'start': 826.559, 'duration': 3.963}, {'end': 834.666, 'text': "Now there's one other way that we could fix this, and that's probably what I would do in this situation.", 'start': 830.703, 'duration': 3.963}, {'end': 838.289, 'text': 'And that is just move the definition of this into one translation unit.', 'start': 834.826, 'duration': 3.463}], 'summary': 'Fixing the issue by marking the log function as static or inline, or moving its definition into one translation unit to avoid linking errors.', 'duration': 71.611, 'max_score': 766.678, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/H4s55GgAg0I/pics/H4s55GgAg0I766678.jpg'}, {'end': 889.666, 'src': 'heatmap', 'start': 863.2, 'weight': 0.768, 'content': [{'end': 868.486, 'text': "And then I'll come back to my log.h and just leave the declaration here again without the inline, of course.", 'start': 863.2, 'duration': 5.286}, {'end': 871.228, 'text': 'So now this header file just has the declaration for log.', 'start': 868.706, 'duration': 2.522}, {'end': 880.037, 'text': 'The actual function to link to is included inside log.cpp once in one translation unit in our project, and then main will call that.', 'start': 871.348, 'duration': 8.689}, {'end': 885.343, 'text': 'So if I build this, we get no linking errors and our project was able to be linked successfully.', 'start': 880.438, 'duration': 4.905}, {'end': 886.283, 'text': "So that's it.", 'start': 885.763, 'duration': 0.52}, {'end': 889.666, 'text': "That's pretty much a quick crash course on linking and how linking works.", 'start': 886.344, 'duration': 3.322}], 'summary': 'Log function declaration in header file avoids linking errors, allowing successful project linking.', 'duration': 26.466, 'max_score': 863.2, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/H4s55GgAg0I/pics/H4s55GgAg0I863200.jpg'}, {'end': 935.783, 'src': 'embed', 'start': 909.98, 'weight': 3, 'content': [{'end': 911.381, 'text': "There's also different types of linking.", 'start': 909.98, 'duration': 1.401}, {'end': 915.184, 'text': "We have static linking and we have dynamic linking, but I'll save that for another video.", 'start': 911.441, 'duration': 3.743}, {'end': 916.505, 'text': 'Anyway, thanks for watching.', 'start': 915.464, 'duration': 1.041}, {'end': 917.586, 'text': 'I hope you enjoyed this video.', 'start': 916.545, 'duration': 1.041}, {'end': 922.17, 'text': "If you have any other questions, just leave them in the comment section below and I'll try my best to answer them.", 'start': 917.946, 'duration': 4.224}, {'end': 924.272, 'text': 'Be sure to follow me on Twitter and Instagram.', 'start': 922.331, 'duration': 1.941}, {'end': 926.935, 'text': 'And if you really like this video, you can support me on Patreon.', 'start': 924.513, 'duration': 2.422}, {'end': 928.596, 'text': "That'll help me make more of these videos.", 'start': 927.135, 'duration': 1.461}, {'end': 933.461, 'text': "And by doing so, you'll also get access to early drafts of videos and be involved in the planning process.", 'start': 928.837, 'duration': 4.624}, {'end': 935.303, 'text': "But as always, I'll see you guys next time.", 'start': 933.641, 'duration': 1.662}, {'end': 935.783, 'text': 'Goodbye.', 'start': 935.463, 'duration': 0.32}], 'summary': 'The video covers different types of linking and encourages viewers to engage and support through social media and patreon.', 'duration': 25.803, 'max_score': 909.98, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/H4s55GgAg0I/pics/H4s55GgAg0I909980.jpg'}], 'start': 582.975, 'title': 'Linking errors and solutions', 'summary': 'Discusses the issue of duplicate symbols in a program, potential compilation errors, and solutions in c++ linking, with a focus on careful management and practical techniques to ensure successful project linking.', 'chapters': [{'end': 654.417, 'start': 582.975, 'title': 'Linking errors and duplicate symbols', 'summary': 'Discusses the issue of duplicate symbols in a program, where identical functions or variables with the same name and signature can cause ambiguity for the linker, leading to potential compilation errors and the need for careful management in separate files.', 'duration': 71.442, 'highlights': ['The presence of duplicate symbols, such as identical named functions with the same return value and parameters, can lead to ambiguity for the linker and potential trouble in the compilation process.', 'Copying and pasting a function with the same name and signature can cause a compile error due to the ambiguity for the linker.', 'Moving the duplicate symbol into a different file can mask the issue during compilation, highlighting the importance of managing duplicate symbols across files to avoid potential linking errors.']}, {'end': 935.783, 'start': 654.417, 'title': 'Understanding linking in c++', 'summary': 'Explains the issue of multiply defined symbols in c++ linking, demonstrates how it can occur, and provides solutions such as marking the function as static or inline and moving the definition to one translation unit, ultimately ensuring successful linking of the project without errors.', 'duration': 281.366, 'highlights': ['The chapter explains how the issue of multiply defined symbols in C++ linking can occur, despite having only one definition of a function, and provides solutions such as marking the function as static or inline and moving the definition to one translation unit, ultimately ensuring successful linking of the project without errors.', 'The transcript details how the issue of multiply defined symbols in C++ linking can occur, and provides solutions such as marking the function as static or inline and moving the definition to one translation unit, ultimately ensuring successful linking of the project without errors.', 'The chapter illustrates the issue of multiply defined symbols in C++ linking, demonstrates how it can occur, and provides solutions such as marking the function as static or inline and moving the definition to one translation unit, ultimately ensuring successful linking of the project without errors.', 'The chapter discusses the different types of linking, including static and dynamic linking, and hints at a future video topic, while also encouraging audience engagement and support through social media and Patreon.']}], 'duration': 352.808, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/H4s55GgAg0I/pics/H4s55GgAg0I582975.jpg', 'highlights': ['The presence of duplicate symbols can lead to ambiguity for the linker and potential trouble in the compilation process.', 'Moving the duplicate symbol into a different file can mask the issue during compilation, highlighting the importance of managing duplicate symbols across files.', 'The issue of multiply defined symbols in C++ linking can occur, despite having only one definition of a function, and provides solutions such as marking the function as static or inline and moving the definition to one translation unit.', 'The chapter discusses the different types of linking, including static and dynamic linking, and hints at a future video topic.']}], 'highlights': ["The C++ linker's primary focus is to find and link symbols and functions from separate object files into one program, even if all functions are in one file.", 'The chapter demonstrates a simple project in Visual Studio containing one source file with two functions, log and multiply, and explains the two stages of compilation: compiling and linking.', 'Understanding and addressing linking errors by ensuring matching function signatures, including return type and parameters, to avoid unresolved external symbol errors.', 'Resolving compile errors by adding necessary includes like iostream and declaring the log function in the math.cpp file to ensure successful compilation.', 'Successfully generating the EXC file after writing the main function and building the project, eliminating the linking error.', "Demonstrating the application running correctly by logging the message 'multiply' and the value 40, ensuring proper functionality.", 'Verifying the correct output of the multiply function by multiplying 5 and 8, obtaining the value 40 as expected.', "Utilizing the 'static' keyword to limit the scope of a function to a specific translation unit in order to eliminate linking errors for unused functions.", 'Separating logging functions into a separate file called log.cpp to organize code and improve maintainability.', 'The presence of duplicate symbols can lead to ambiguity for the linker and potential trouble in the compilation process.', 'Moving the duplicate symbol into a different file can mask the issue during compilation, highlighting the importance of managing duplicate symbols across files.', 'The issue of multiply defined symbols in C++ linking can occur, despite having only one definition of a function, and provides solutions such as marking the function as static or inline and moving the definition to one translation unit.', 'The chapter discusses the different types of linking, including static and dynamic linking, and hints at a future video topic.', 'The chapter highlights the differentiation between compilation and linking errors in Visual Studio, emphasizing that hitting Control F7 or the compile button triggers only compilation, while building the project or hitting F5 compiles and links the code.', "It emphasizes the need to know the type of error to fix it properly, providing the example of an 'entry point must be defined' error that occurs due to compiling an application without a specified entry point."]}