Skip to main content

There is a great difference between C++ and Java.

Java operates on his own virtual machine that creates everything into its own run-time environment. If a definition happens to be seen more than once, will simply act on the same object the runtime environment ultimatelly knows.

In C++ there is no "ultimate knowledge owner": C++, C, Fortran Pascal etc. are all "translator" from a source code (CPP file) into an intermediate format (the OBJ file, or ".o" file, depending on the OS) where statements are translated into machine instruction and names becomes indirect addresses mediated by a symbol table.

A program is not made by the compiler, but by another program (the "linker"), that joins all OBJ-s together (no matter the language they come from) by re-pointing all the addresses that are towards symbols, towards their effective definition.

By the way the linker works, a definition (what creates the physical space for a variable) must be unique.

Note that C++ does not by itself link, and that the linker is not issued by C++ specs: the linker exist because of the way the OS modules are built up (usually in C and ASM). C++ has to use it the way it is.

Now: ana header file is something to be "pasted into" several CPP files. Every CPP file is translated independently on whateverof every other one. A copilercompiler translating different CPP files, all receiving-in a same definition will place the "creation code" for the defined object in all the resulting OBJs.

The compiler doesn't know (and will never know) if all those OBJs will ever be used together to form a single program or separately to form different independent programs.

The linker doesn't know how and why definitions exist and where they come from (it even doesn't know about C++: every "static language" can produce definitions and references to be linked). It just knows there are some referencereferences to a given "symbol" that is "defined" at a given resulting address.

If there are multiple definitiondefinitions (don't confuse definitions with references) for a samegiven symbol exist, the linker has no knowledge (being language agnostic) about what to do with them.

It is like merging a number of city to form a big town: if you're found having two "Time square" and a nubernumber of people coming from outside asking to go to "Time square", you cannot decide on a pure technical basebasis (without any knowledge about the politics that assigned those names and will be in charge to manage them) in which exact place to send them.

There is a great difference between C++ and Java.

Java operates on his own virtual machine that creates everything into its own run-time environment. If a definition happens to be seen more than once, will simply act on the same object the runtime environment ultimatelly knows.

In C++ there is no "ultimate knowledge owner": C++, C, Fortran Pascal etc. are all "translator" from a source code (CPP file) into an intermediate format (the OBJ file, or ".o" file, depending on the OS) where statements are translated into machine instruction and names becomes indirect addresses mediated by a symbol table.

A program is not made by the compiler, but by another program (the "linker"), that joins all OBJ-s together (no matter the language they come from) by re-pointing all the addresses that are towards symbols, towards their effective definition.

By the way the linker works, a definition (what creates the physical space for a variable) must be unique.

Note that C++ does not by itself link, and that the linker is not issued by C++ specs: the linker exist because of the way the OS modules are built up (usually in C and ASM). C++ has to use it the way it is.

Now: an header file is something to be "pasted into" several CPP files. Every CPP file is translated independently on whatever other. A copiler translating different CPP files, all receiving-in a same definition will place the "creation code" for the defined object in all the resulting OBJs.

The compiler doesn't know (and will never know) if all those OBJs will ever be used together to form a single program or separately to form different independent programs.

The linker doesn't know how and why definitions exist and where they come from (it even doesn't know about C++: every "static language" can produce definitions and references to be linked). It just knows there are some reference to a given "symbol" that is "defined" at a given resulting address.

If multiple definition (don't confuse definitions with references) for a same symbol exist, the linker has no knowledge (being language agnostic) about what to do with them.

It is like merging a number of city to form a big town: if you're found having two "Time square" and a nuber of people coming from outside asking to go to "Time square", you cannot decide on a pure technical base (without any knowledge about the politics that assigned those names and will be in charge to manage them) in which exact place to send them.

There is a great difference between C++ and Java.

Java operates on his own virtual machine that creates everything into its own run-time environment. If a definition happens to be seen more than once, will simply act on the same object the runtime environment ultimatelly knows.

In C++ there is no "ultimate knowledge owner": C++, C, Fortran Pascal etc. are all "translator" from a source code (CPP file) into an intermediate format (the OBJ file, or ".o" file, depending on the OS) where statements are translated into machine instruction and names becomes indirect addresses mediated by a symbol table.

A program is not made by the compiler, but by another program (the "linker"), that joins all OBJ-s together (no matter the language they come from) by re-pointing all the addresses that are towards symbols, towards their effective definition.

By the way the linker works, a definition (what creates the physical space for a variable) must be unique.

Note that C++ does not by itself link, and that the linker is not issued by C++ specs: the linker exist because of the way the OS modules are built up (usually in C and ASM). C++ has to use it the way it is.

Now: a header file is something to be "pasted into" several CPP files. Every CPP file is translated independently of every other one. A compiler translating different CPP files, all receiving-in a same definition will place the "creation code" for the defined object in all the resulting OBJs.

The compiler doesn't know (and will never know) if all those OBJs will ever be used together to form a single program or separately to form different independent programs.

The linker doesn't know how and why definitions exist and where they come from (it even doesn't know about C++: every "static language" can produce definitions and references to be linked). It just knows there are references to a given "symbol" that is "defined" at a given resulting address.

If there are multiple definitions (don't confuse definitions with references) for a given symbol, the linker has no knowledge (being language agnostic) about what to do with them.

It is like merging a number of city to form a big town: if you're found having two "Time square" and a number of people coming from outside asking to go to "Time square", you cannot decide on a pure technical basis (without any knowledge about the politics that assigned those names and will be in charge to manage them) in which exact place to send them.

Source Link
Emilio Garavaglia
  • 4.3k
  • 1
  • 23
  • 23

There is a great difference between C++ and Java.

Java operates on his own virtual machine that creates everything into its own run-time environment. If a definition happens to be seen more than once, will simply act on the same object the runtime environment ultimatelly knows.

In C++ there is no "ultimate knowledge owner": C++, C, Fortran Pascal etc. are all "translator" from a source code (CPP file) into an intermediate format (the OBJ file, or ".o" file, depending on the OS) where statements are translated into machine instruction and names becomes indirect addresses mediated by a symbol table.

A program is not made by the compiler, but by another program (the "linker"), that joins all OBJ-s together (no matter the language they come from) by re-pointing all the addresses that are towards symbols, towards their effective definition.

By the way the linker works, a definition (what creates the physical space for a variable) must be unique.

Note that C++ does not by itself link, and that the linker is not issued by C++ specs: the linker exist because of the way the OS modules are built up (usually in C and ASM). C++ has to use it the way it is.

Now: an header file is something to be "pasted into" several CPP files. Every CPP file is translated independently on whatever other. A copiler translating different CPP files, all receiving-in a same definition will place the "creation code" for the defined object in all the resulting OBJs.

The compiler doesn't know (and will never know) if all those OBJs will ever be used together to form a single program or separately to form different independent programs.

The linker doesn't know how and why definitions exist and where they come from (it even doesn't know about C++: every "static language" can produce definitions and references to be linked). It just knows there are some reference to a given "symbol" that is "defined" at a given resulting address.

If multiple definition (don't confuse definitions with references) for a same symbol exist, the linker has no knowledge (being language agnostic) about what to do with them.

It is like merging a number of city to form a big town: if you're found having two "Time square" and a nuber of people coming from outside asking to go to "Time square", you cannot decide on a pure technical base (without any knowledge about the politics that assigned those names and will be in charge to manage them) in which exact place to send them.