What is external linkage and internal linkage?

前端 未结 9 1213
情深已故
情深已故 2020-11-22 00:10

I want to understand the external linkage and internal linkage and their difference.

I also want to know the meaning of

const va

相关标签:
9条回答
  • 2020-11-22 00:10

    When you write an implementation file (.cpp, .cxx, etc) your compiler generates a translation unit. This is the source file from your implementation plus all the headers you #included in it.

    Internal linkage refers to everything only in scope of a translation unit.

    External linkage refers to things that exist beyond a particular translation unit. In other words, accessible through the whole program, which is the combination of all translation units (or object files).

    0 讨论(0)
  • 2020-11-22 00:11

    In terms of 'C' (Because static keyword has different meaning between 'C' & 'C++')

    Lets talk about different scope in 'C'

    SCOPE: It is basically how long can I see something and how far.

    1. Local variable : Scope is only inside a function. It resides in the STACK area of RAM. Which means that every time a function gets called all the variables that are the part of that function, including function arguments are freshly created and are destroyed once the control goes out of the function. (Because the stack is flushed every time function returns)

    2. Static variable: Scope of this is for a file. It is accessible every where in the file
      in which it is declared. It resides in the DATA segment of RAM. Since this can only be accessed inside a file and hence INTERNAL linkage. Any
      other files cannot see this variable. In fact STATIC keyword is the only way in which we can introduce some level of data or function
      hiding in 'C'

    3. Global variable: Scope of this is for an entire application. It is accessible form every where of the application. Global variables also resides in DATA segment Since it can be accessed every where in the application and hence EXTERNAL Linkage

    By default all functions are global. In case, if you need to hide some functions in a file from outside, you can prefix the static keyword to the function. :-)

    0 讨论(0)
  • 2020-11-22 00:15

    As dudewat said external linkage means the symbol (function or global variable) is accessible throughout your program and internal linkage means that it's only accessible in one translation unit.

    You can explicitly control the linkage of a symbol by using the extern and static keywords. If the linkage isn't specified then the default linkage is extern for non-const symbols and static (internal) for const symbols.

    // in namespace or global scope
    int i; // extern by default
    const int ci; // static by default
    extern const int eci; // explicitly extern
    static int si; // explicitly static
    
    // the same goes for functions (but there are no const functions)
    int foo(); // extern by default
    static int bar(); // explicitly static 
    

    Note that instead of using static for internal linkage it is better to use anonymous namespaces into which you can also put classes. The linkage for anonymous namespaces has changed between C++98 and C++11 but the main thing is that they are unreachable from other translation units.

    namespace {
       int i; // external linkage but unreachable from other translation units.
       class invisible_to_others { };
    }
    
    0 讨论(0)
  • 2020-11-22 00:18

    Linkage determines whether identifiers that have identical names refer to the same object, function, or other entity, even if those identifiers appear in different translation units. The linkage of an identifier depends on how it was declared. There are three types of linkages:

    1. Internal linkage : identifiers can only be seen within a translation unit.
    2. External linkage : identifiers can be seen (and referred to) in other translation units.
    3. No linkage : identifiers can only be seen in the scope in which they are defined. Linkage does not affect scoping

    C++ only : You can also have linkage between C++ and non-C++ code fragments, which is called language linkage.

    Source :IBM Program Linkage

    0 讨论(0)
  • 2020-11-22 00:19

    In C++

    Any variable at file scope and that is not nested inside a class or function, is visible throughout all translation units in a program. This is called external linkage because at link time the name is visible to the linker everywhere, external to that translation unit.

    Global variables and ordinary functions have external linkage.

    Static object or function name at file scope is local to translation unit. That is called as Internal Linkage

    Linkage refers only to elements that have addresses at link/load time; thus, class declarations and local variables have no linkage.

    0 讨论(0)
  • 2020-11-22 00:23

    Basically

    • extern linkage variable is visible in all files
    • internal linkage variable is visible in single file.

    Explain: const variables internally link by default unless otherwise declared as extern

    1. by default, global variable is external linkage
    2. but, const global variable is internal linkage
    3. extra, extern const global variable is external linkage

    A pretty good material about linkage in C++

    http://www.goldsborough.me/c/c++/linker/2016/03/30/19-34-25-internal_and_external_linkage_in_c++/

    0 讨论(0)
提交回复
热议问题