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.