0% found this document useful (0 votes)
153 views3 pages

03 - How The C++ Linker Works

The C++ linker works by finding where each symbol and function is defined and linking them together. Source files are compiled separately into object files that have no relationship to each other. The linker resolves external references by locating the entry point, usually the main function, and linking other functions and symbols that are called. Linking errors occur when the linker cannot find an external symbol or function that is needed.

Uploaded by

22194
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
153 views3 pages

03 - How The C++ Linker Works

The C++ linker works by finding where each symbol and function is defined and linking them together. Source files are compiled separately into object files that have no relationship to each other. The linker resolves external references by locating the entry point, usually the main function, and linking other functions and symbols that are called. Linking errors occur when the linker cannot find an external symbol or function that is needed.

Uploaded by

22194
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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>

void Log(const char* message);

int Multiply(int a, int b)


{
Log("Multiply");
return a * b;
}

int main()
{
std::cout << Multiply(5, 8) << std::endl;
std::cin.get();

// int Multiply(int a, int b)


no error

// std::cout << Multiply(5, 8) << std::endl;


error
you could still use multiply in other files
linker needs to link that function

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

fixing multiple definitions


• static void Log(const char* message)
– linking that happens to that log function
– should only be internal
– when log gets included into log.cpp and math.cpp
• will be just internal to that file
– log and math will have their own version
• inline void Log(const char* message)
– take function body
– replace the call with it
• move the definition into one translation unit
– put it into a third translation unit?
– put the log definition into one of
• the existing translation units
• linking
– static
– dynamic
• linking role
– c runtime library
– c++ standart library
– platform API
• if necesarry

You might also like