zoukankan      html  css  js  c++  java
  • Name mangling

    https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_72/rzarg/name_mangling.htm

    Name mangling is the encoding of function and variable names into unique names so that linkers can separate common names in the language. Type names may also be mangled. Name mangling is commonly used to facilitate the overloading feature and visibility within different scopes. The compiler generates function names with an encoding of the types of the function arguments when the module is compiled. If a variable is in a namespace, the name of the namespace is mangled into the variable name so that the same variable name can exist in more than one namespace. The C++ compiler also mangles C variable names to identify the namespace in which the C variable resides.

    The scheme for producing a mangled name differs with the object model used to compile the source code: the mangled name of an object of a class compiled using one object model will be different from that of an object of the same class compiled using a different object model. The object model is controlled by compiler option or by pragma.

    Name mangling is not desirable when linking C modules with libraries or object files compiled with a C++ compiler. To prevent the C++ compiler from mangling the name of a function, you can apply theextern "C"linkage specifier to the declaration or declarations, as shown in the following example:
    extern "C" {
       int f1(int);
       int f2(int);
       int f3(int);
    };
    This declaration tells the compiler that references to the functionsf1,f2, andf3should not be mangled.
    Theextern "C"linkage specifier can also be used to prevent mangling of functions that are defined in C++ so that they can be called from C. For example,
    extern "C" {
       void p(int){
          /* not mangled */
       }
    };
    In multiple levels of nestedexterndeclarations, the innermostexternspecification prevails.
    extern "C" {
          extern "C++" {
                void func();
          }
    }
    In this example,funchas C++ linkage.
     
     
     
     
     
     
    C does not do name mangling, though it does pre-pend an underscore to function names,
    so the printf(3) is actually _printf in the libc object.

    In C++ the story is different. The history of it is that originally
    Stroustrup created "C with classes" or cfront, a compiler that would translate early C++ to C.
    Then rest of the tools - C compiler and linker would we used to produce object code.
    This implied that C++ names had to be translated to C names somehow.
    This is exactly what name mangling does.
    It provides a unique name for each class member and global/namespace function and variable,
    so namespace and class names (for resolution) and argument types (for overloading) are
    somehow included in the final linker names.

    This is very easy to see with tools like nm(1) -
    compile your C++ source and look at the generated symbols. The following is on OSX with GCC:

    namespace zoom
    {
        void boom( const std::string& s )
        {
            throw std::runtime_error( s )<U+003B>
        }
    }

    ~$ nm a.out | grep boom
    0000000100001873 T __ZN4zoom4boomERKSs

    In both C and C++ local (automatic) variables produce no symbols,
    but live in registers or on stack.

    Local variables do not have names in resulting object file for mere reason that
    linker does not need to know about them. So no name, no mangling.
    Everything else (that linker has to look at) is name-mangled in C++.



    Mangling is simply how the compiler keeps the linker happy.
    In C, you can't have two functions with the same name, no matter what.
    So that's what the linker was written to assume: unique names.
    (You can have static functions in different compilation units,
    because their names aren't of interest to the linker.)

    In C++, you can have two functions with the same name as long as
    they have different parameter types.
    So C++ combines the function name with the types in some way.
    That way the linker sees them as having different names.

    The exact manner of mangling is not significant to the programmer, only the compiler,
    and in fact every compiler does it differently.
    All that matters is that every function with the same base name is somehow made unique for the linker.

    You can see now that adding namespaces and templates to the mix keeps extending the principle.
  • 相关阅读:
    kafka注册异常
    Android基于XMPP Smack Openfire下学习开发IM(五)连接断开重连
    openfire维持在线状态,监听消息
    openfire ping的smack解决方案(维持在线状态)
    openfire聊天记录插件
    openfire 发送 接受 注册 广播 好友列表 在线状态
    maven仓库中心mirrors配置多个下载中心(执行最快的镜像)
    开发openfire 消息拦截器插件PacketInterceptor
    Openfire注册流程代码分析
    linux centOS6 nexus 开启自动启动
  • 原文地址:https://www.cnblogs.com/Searchor/p/14023795.html
Copyright © 2011-2022 走看看