Mikhail Polatov said:
3.5.2 A name is said to have linkage when it might denote the same
object, reference, function, type, template, namespace or value as a
name introduced by a declaration in another scope:
- When a name has external linkage, the entity it denotes can be
referred to by names from scopes of other translation units or from
other scopes of the same translation unit.
- When a name has internal linkage, the entity it denotes can be
referred to by names from other scopes in the same translation unit.
- When a name has no linkage, the entity it denotes cannot be referred
to by names from other scopes.
Mikhail,
Thank you for explaining the difference between internal linkage and
external linkage. I still don't understand your comment. I think that
external linkage means to call variables or functions from first file to
another second file. Internal linkage means to bind all local variables and
local functions inside struct, class, or namespace. Also, another meaning
is to hold global variables and global functions inside one file which they
are inaccessible to other files.
For Example:
// First_File.h
#ifndef FIRST_FILE.H
#define FIRST_FILE.H
extern int Obj1;
extern int Obj2;
extern int Obj3;
extern void Read_Obj(void);
#endif
// First_File.cpp
#include "First_File.h"
#include <stdio.h>
int Obj1 = 0;
int Obj2 = 0;
int Obj3 = 0;
void Read_Obj(void)
{
for (int a = 0; a < 10; ++a)
{
Obj1 += 1;
Obj2 += 2;
Obj3 += 3;
}
printf("Obj1: %d\nObj2: %d\nObj3: %d\n", Obj1, Obj2, Obj3);
}
// Second_File.cpp
#include "First_File.h"
int main(void)
{
Read_Obj();
return 0;
}
My example source code above is external linkage. Please give me your
example source which it is the internal linkage including static. I always
add namespace each header files and source code files to prevent overwritten
existing global variables according to my choice, but I can use struct or
class if I want on depend of my design.
The problem is that Read_Obj() function can't be forced inline. It is
not the C++ Compiler's decision to grant or deny __forceinline because
Read_Obj() function has external linkage. It creates two object files such
as *.obj. It is impossible to copy Read_Obj() function from first *.obj to
second *.obj which it pastes into main() function.
If Read_Obj() function is placed in Second_File.cpp rather than
First_File.cpp, __forceinline will work while C++ Compiler can always grant
or deny __forceinline because Read_Obj() function has internal linkage.
I used three different C++ Compiler such as Microsoft Visual C++, Intel
C++ Compiler, and GCC Compiler, but their results are always the same.
Is my concept explanation correct?
Bryan Parkoff