0

Say I have a file called main.h and in there I have some functions declared like

void func();

Then I have a file main.cpp with the definitions. And then finally I have a file called a.cpp which includes main() and just runs all these functions BUT I also have like some variables declared in it like this:

string a = "t";
int b = 2;

Now my question is the linker will combine all this into one executable right? But I mean the variables a and b do they get linked to anything? I mean they already have a definition right and somehow all this code gets combined to one executable right? So is it just fair to say the linker also kind of just combines all the code from all 3 files? It sees declarations and finds definitions for them but with these variables and stuff they don't really have anything to link to so does the linker even do anything to them? Or like I said earlier is it correct to say that the linker basically combines all this code together?

stefaanv
  • 14,072
  • 2
  • 31
  • 53
jaylse2
  • 71
  • 4
  • 1
    Roughly : The compiler compiles source files to intermediate binary files (object files) one for each cpp file. To do this it uses header files to know what symbols to expect from other parts of the code. (detail : in case of templates/inline functions also takes sources from those header files). So func ends up in somefile.obj and a and b will end up in main.obj. And the linker will then combine them into an executable (or statically or dynamically linkable library) – Pepijn Kramer Oct 19 '21 at 07:57

2 Answers2

2

On a language point of view, this is called scope. Functions and variables in global scope should respect the One Definition Rule. That means that they can be declared in any compilation units but must be defined exactly once in the program.

For functions a declaration without body (void func();) is not a definition and can be used in all compilation units, even in the one containing the definition.

For variables, you must use the extern keyword (extern int b;) to have a declaration which is not a definition.

The same rules also apply for named scope: inside any named scope, functions and variables should respect the One Definition Rule.


On a linker point of view, things are slightly more complex because a function is more than one name: it also contains its return and parameter types: int func(void); and int func(int); declare two different functions. Common implemantation use name mangling to build a complex symbol containing the name and the types and optionaly the scope. The compiler fills a symbol table to declare whether the symbol is defined locally or should be resolved by the linker and the linker assigns a (physical or virtual) address to those symbols to produce an executable file.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

First, the compiler compile all sources file then linker link all object files into one executable file.

then, the linker checks the linkage of global variable and function declaration in another translation unit.

Jitu DeRaps
  • 144
  • 1
  • 9