Hi, I'm new to C++ and I was wondering what the linker does. I'm currently using "Sams Teach Yourself C++ In and Hour a Day" but he doesn't explain it clear enough. Thanks for the help.
A compiler takes a .cpp file, adds the text of all its #includes (.h files) and compiles the result (which is called a compilation unit) into a .o file (object code). After all the compilation units are compiled, the linker takes the .o files and links them together into one file. It matches declarations to their definitions and catches the error of forgetting to add a definition to back up a declaration.
Thanks for the help. I don't get what a declaration or definition is so could you please explain those to me?
Also another thing I don't get is I thought you only have one source code, how does the compiler make so many ".o" files?
A function declaration is a statement that tells the compiler the name of a function, what kind of parameters it will take in, and what kind of parameter it will return.
For example,
int addTwoNumbers (int numberOne, int numberTwo);
declares that there is this function called addTwoNumbers , and it takes two parameters, which are in this case both of type int, and it will return an int. The code for how it will actually work has not been written here. In effect, you have promised the compiler that the actual function code exists somewhere (maybe you'll write it yourself, maybe it's in a library somewhere). If the time comes to link to this function and it's never actually been made, you'll get a linker error.
A function definition is the code that actually does the work. Here, for example, is the definition of the function that I declared above.
1 2 3 4 5
int addTwoNumbers (int numberOne, int numberTwo)
{
int numberToReturn = numberOne+numberOne;
return numberToReturn;
}
If you are using an already compiled library, the header file it comes with will usually contain a number of declarations. You include the header, and now your compiler "knows" what all those functions look like (i.e. their name, their input and parameters) so you can make calls to them in your own code. Then, at linker time, the linker will link to the already compiled definitions in the provided compiled library.
Could you explain to me what a parameter is?
Also I don't know what a header file is either is it the files that end with the extension ".h"? What do header files do?
I thought you already made a function definition, so how come the compiler still needs a header file to know what the function looks like?
Thanks for the help though I'm starting to get the idea of what the linker does.
A parameter in this context is what you pass in to the function. In that sample function above, there are two parameters; they are both of type int.
I thought you already made a function definition, so how come the compiler still needs a header file to know what the function looks like?
Because you don't have to make a function definition. Maybe someone else has done that for you in a library that someone else has already compiled and provided to you. Your compiler needs to know what to pass into the function and what to expect back, so you need the declaration, but you won't be defining it. Have any of your programmes so far used cout, or printf? You didn't define those but it still worked.
If you read the first few pages of the tutorial all this will become much clearer.
Just about; the term "library" tends to commonly be applied to the header file and/or the source code that goes with it and/or the compiled binary file, so depending on context by library you might mean any one or more of those items, but yes - for practical purposes #include<iostream> essentially declares all the functions and types and classes and so on available in that library, making them available for you to use, although the actual definitions have probably been compiled already and are sitting in a library binary ready to be linked to.
The linker then has to be told which compiled libraries to link against, so it knows where to go looking for thee classes and functions and so on. When you see the linker error "Undefined reference" you know that you've included the headers, so your compiler knows what these functions look like and is happy to compile, but the linker has been unable to find them already compiled in a library somewhere.