What does mean for a name or type to have a certain language linkage?

前端 未结 7 2179
南笙
南笙 2021-02-01 02:14

According to (c) ANSI ISO/IEC 14882:2003, page 127:

Linkage specifications nest. When linkage specifications nest, the innermost one determines the langu

7条回答
  •  忘了有多久
    2021-02-01 02:38

    It has to do with the ABI (Application Binary Interface) of the program.

    As an API specifies the the external interface of the source code of a program, an ABI specifies the external interface of the binary code of the program (the compiled version).


    Originally, C functions simply had a few different forms. Something like

    int foo(int);
    

    would be prefixed by an underscore by the compiler, to form _foo, and then exported to be made available to other applications.

    However, that wasn't enough. If you look at the Windows API, for instance, you will see things like:

    DWORD CreateWindowW(...);        //Original parameters
    DWORD CreateWindowExW(..., ...); //More parameters
    

    This is because there's no way to distinguish between the overloads of a function simply by looking at the name of the function, so people started changing them by adding an Ex suffix (or the like).

    This grew to be pretty ugly, and it still didn't allow for operator overloading, which was featured in C++. Because of this, C++ came up with name mangling, to put extra information in the name of the function, like the data types of its parameters, and making it something cryptic with lots of @ symbols.

    It was all well, except that it wasn't completely standardized.

    Of course, as new languages and compilers came about, each came up with its own scheme, some incompatible with others. So if you need to import or export an external function, you need to specify what kind of ABI the compiler should look for, hence the extern "C++" you have there.

提交回复
热议问题