How does an extern “C” declaration work?

后端 未结 9 793
礼貌的吻别
礼貌的吻别 2020-12-02 14:33

I\'m taking a programming languages course and we\'re talking about the extern \"C\" declaration.

How does this declaration work at a deeper level othe

9条回答
  •  心在旅途
    2020-12-02 15:16

    extern "C", is a keyword to declare a function with C bindings, because C compiler and C++ compiler will translate source into different form in object file:

    For example, a code snippet is as follows:

    int _cdecl func1(void) {return 0}
    int _stdcall func2(int) {return 0}
    int _fastcall func3(void) {return 1}
    

    32-bit C compilers will translate the code in the form as follows:

    _func1
    _func2@4
    @func3@4
    

    in the cdecl, func1 will translate as '_name'

    in the stdcall, func2 will translate as '_name@X'

    in the fastcall, func2 will translate as '@name@X'

    'X' means the how many bytes of the parameters in parameter list.

    64-bit convention on Windows has no leading underscore

    In C++, classes, templates, namespaces and operator overloading are introduced, since it is not allowed two functions with the same name, C++ compiler provide the type information in the symbol name,

    for example, a code snippet is as follows:

    int func(void) {return 1;}
    int func(int) {return 0;}
    int func_call(void) {int m=func(), n=func(0);}
    

    C++ compiler will translate the code as follows:

    int func_v(void) {return 1;}
    int func_i(int) {return 0;}
    int func_call(void) {int m=_func_v(), n=_func_i(0);}
    

    '_v' and '_i' are type information of 'void' and 'int'

提交回复
热议问题