03 - How The C++ Linker Works
03 - How The C++ Linker Works
• linking role
– find where each symbol and function is
• link them together
– each file copiled separately into an obj file
• as a translation unit
– they have no relationship
• no functions in external files
– application still needs to know where the entry point is
– aka where the main funtion is
– C runtime library
• can identify main function
• start from there
• entry point must be defined
– no main function detected
• error messages
– compiler error:
• ex: “;”
• C2143
• error that occured in compiler stage
– link stage error
• LK32432
• entry point must be defined
• linker>advanded>specify entry point
– doesnt necesarily need to be a main fucntion
– it can be anything
• linking error
– uresolved external symbol
– linker can’t find what it needs
– in log.cpp: log->logr
• the compiling is done right
• but the build won’t work
#include <iostream>
int main()
{
std::cout << Multiply(5, 8) << std::endl;
std::cin.get();
BUT IF
static int Multiply(int a, int b)
{
Log("Multiply");
return a * b;
}
• static
– multiply function is only declared in this translation unit
– this cpp file
• also errors if don’t match
– functin types
– number of parameters
• duplicate symbols
– fucntions and variables that have the same name and same signature
• same return value
• same parameters
– linker does not know which one to link to
• compiler error
– if duplicate is in the same file
• linker error
– if duplicate is in different files
• declarations and definitions
– do not interfere