What are Linker Errors?

Tim Buchalka's Learn Programming Academy
A free video tutorial from Tim Buchalka's Learn Programming Academy
Professional Programmers and Teachers - 1.1M students
4.5 instructor rating • 52 courses • 1,184,945 students

Lecture description

In this video we discuss linker errors and what causes them.

Learn more from the full course

Beginning C++ Programming - From Beginner to Beyond

Obtain Modern C++ Object-Oriented Programming (OOP) and STL skills needed for game, system, and application development.

41:29:30 of on-demand video • Updated May 2021

  • Learn to program with one of the most powerful programming languages that exists today, C++.
  • Obtain the key concepts of programming that will also apply to other programming languages
  • Learn Modern C++ rather than an obsolete version of C++ that most other courses teach
  • Learn C++ features from basic to more advanced such as inheritance and polymorphic functions
  • Learn C++ using a proven curriculum that covers more material than most C++ university courses
  • Learn C++ from an experienced university full professor who has been using and teaching C++ for more than 25 years
  • Includes Quizzes, Live Coding Exercises, Challenge Coding Exercises and Assignments
English In this video, we'll talk about linker errors and why they can happen. Linker errors occur when the linker is trying to put all the pieces of a program together to create an executable, and one or more pieces are missing. Typically, this can happen when an object file or libraries can't be found by the linker. Fixing linker errors can be tricky. Sometimes it's just a matter of getting the paths correct, so that the linker knows where the missing pieces are. Let's head over to CodeLite. And we'll force a program to have a linker error, so you can see what the error message looks like. So I'm in CodeLite. And I've created a project called linker error based on the template that we use. So all I've got is a main cpp file that says hello world. Now we're going to force a linker error here. This is not something you want to do normally. But I'm going to do it to show you what the error looks like. So what I'm going to say is I'm going to say extern int c or let's just say x. And I'll explain all this to you in just a second. And then in the body of the main, I want to try to use that x right there. It's just an integer. And let's say, I want to print it out. I just want to print out x, simple as that. I'm going to save this. Now you'll see if I compile this program. You'll see what happens. I'm going to right click, compile. I get a clean compile. There's nothing wrong with the program. But when I try to build and run it, you can see the linker error. This is classic linker error, undefined reference to something. In this case, it just happens to be x. Also you'll notice that the linker returned one here, ld returns one, which is the x status. So something was wrong with the link. Obviously, the compile was fine because we got no compiler errors. Let me do that again. There's the compile. No errors. No warnings. So let me explain to you what's going on here. Let me grab my stylus here real quick. And what's going on here is I've got my main cpp file here. And it's going to be compiled to main.o, let's say, or main.object. In here, we have a reference to an x. You can see it right there. You can see it right there. So in here, I've got a reference to an x, which I'm going to use. Now we also have our iostream libraries. So the linker needs to grab those libraries. And I'll just type iostream here. You know what I'm talking about. The linker needs to put these two together to create my main.exe or in this case I believe it would be linker error.exe but you know what I mean. So the compiler is fine. We've created our object file. This library exists. The problem is that I've told the compiler here that this x is not in me. It's somewhere else. It's defined by somebody else. Well, obviously it isn't defined by someone else because that kind of forced the issue here. So now the linker needs to resolve that x, it's saying okay where where do I get that value to print because that's what you asked me to do right here right. I wanted to print the value of x where where is x. It can't find it. That's the linker error. That's exactly what's going on. And let me build this. Again, so you can see the error message one more time. Let's build it and run it. And there's the error message it's saying undefined reference to x you can see exactly what's going on. It can't find x. Now this is not a compiler error. This is a linker error. That's really important to understand. Linker errors are tricky to fix. A lot of times the files that you need to link are missing. They're in the wrong places. You don't have to worry about that using CodeLite. In this course, we're going to basically do everything ourselves. But if you're using libraries from third parties, this is the kind of thing we'll talk about in a more intermediate or advanced course. But I just wanted to show you linker errors. In case you do come up with one, post it in the forum, and we'll work through it together.