"SLIGHTTPD" LIGHTTPD architecture-based server Project Combat (one)-c++ name mangling

Source: Internet
Author: User

In the previous section, we introduced the plugin as a dynamic library loading, where we noted
Function:

void* dlsym(void* handle,constchar* symbol)

Returns the "symbol corresponding address".

Therefore, in our developed plug-in, the Setupplugin and Removeplugin functions need to be added extern "C":

extern"C" Plugin* SetupPlugin(){    returnnew MyPlugin();}extern"C" Plugin* RemovePlugin(Plugin *plugin){    delete plugin;}

Why is it?

This has to mention the name mangling of C + +:

In each C + + program (or library, destination file), all non-static (non-static) functions appear as "symbol" in the binary file. These symbols are unique strings that differentiate functions in programs, libraries, and destination files.

In C, the symbol name is the function name: the symbol name of the Foo function is "foo". This is possible because the names of the two non-static functions must be different.

In C + +, because overloading is allowed (different functions have the same name but different parameters), and there are many features that C does not have-such as classes, member functions, and so on-it is almost impossible to directly use the function name. To solve this problem, C + + uses the so-called name mangling--, which takes the function name and some information (such as the number and size of parameters) together to create strange shapes, which only the compiler understands the symbolic name. For example, Foo after being mangle may look like [email protected]%6^, or even "foo" is not included in the symbol name. Furthermore, the C + + standard does not define how names must be mangle, so each compiler does its name mangling in its own way. Some compilers even replace the mangling algorithm (especially g++ 2.x and 3.x) between different versions. Even if you figure out how your compiler is mangling, you can call functions with Dlsym, but this is compiler-dependent and portability is not strong.

Solution Solutions

Use extern "C"
C + + has a specific keyword used to declare a function with C binding: extern "C". Functions declared with extern "C" will use a function name, just like a C function.

Of course, only non-member functions can be declared as extern "C" and cannot be overloaded.

In this way, they can be dlopen dynamically loaded like a C function.

The addition of the extern "C" qualifier does not imply that C + + code is not available in the function, in fact, it is still a C + + function that can use any C + + feature.

"SLIGHTTPD" LIGHTTPD architecture-based server Project Combat (one)-c++ name mangling

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.