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 code light and we'll force a program to have a linker error so you can see what the
error message looks like.
The example code here, please ignore the syntax.
This is actually C plus plus code, but it's still relevant to linker errors.
Don't worry about the i o stream.
Don't worry about the stdcout.
The main idea here is that link errors are the same in C and C plus plus.
And the coding example here is just trying to demonstrate again a linker error.
So I'm in code light 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 want 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 could 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 exit status.
So something was wrong with the link.
Obviously the compiler was fine because we got no compiler errors.
We 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 dot
zero, let's say, or main dot object.
In here we have a reference to an EXE.
You can see it right there.
So in here I've got a reference to an exe, which I'm going to use.
Now we also have our O stream libraries, so the linker needs to grab those libraries and I'll just
type i o stream here so you'll 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 compile 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 want it 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.
It'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 code light 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 the linker errors in case you do come up with one, post it in the forum
and we'll work through it together.