extern "C" and DEF files.

Source: Internet
Author: User

Reference:

Http://www.cnblogs.com/whiteyun/archive/2011/07/22/2113560.html

Question: If you use a DEF file to export the function of a DLL, do you also need to precede the exported function declaration with extern "C"?

DEMO Download: Http://pan.baidu.com/s/1kTTY6rH

The purpose of extern "C" is to tell the C + + compiler not to rename the function name (in order to support function overloading), but instead to modify the function name in the manner of the C compiler.

extern "C" and __declspec (dllexport) only need to appear in the header file if the function is specifically declared with a header file, which is optional in the. cpp file that defines the function.

extern "C" and __declspec (dllexport) must appear in the. cpp file that defines the function if no header file is specifically declared for the function.

The above rules also apply to __declspec (dllexport) in pure C projects.

To do the experiment, the idea is as follows, that is to see the various configurations under the Hello.h need not add extern "C" thereby UI2. EXE can find the Fnhello in Hello.dll through GetProcAddress ("Fnhello"):

Note: The Hello project has a Hello.h

1 UI2. EXE compiled as C + + project, HELLO. DLLs are compiled into C + + projects, the C + + compiler version is the same, Hello.h, with __declspec (dllexport):2 Hello.h has extern "C", success, Dumpbin/exports Hello.dll get _fnhello3 Hello.h no extern "C", failed , dumpbin/exports hello.dll get [email protected] @YAXXZ4 UI2. EXE compiled as C + + project, HELLO. DLL compiled as C + + project, C + + compiler version is the same, Hello.h, no __declspec (dllexport), with DEF file:5 Hello.h has extern "C", success, Dumpbin/exports Hello.dll get _fnhello6 Hello.h no extern "C", success, Dumpbin/exports hello.dll get [email protected] @YAXXZ7 UI2. EXE compiled as C + + project, HELLO. DLL compiled to C project, Hello.h, with __declspec (dllexport):8 Hello.h has extern "C", success, Dumpbin/exports Hello.dll get _fnhello9 Hello.h no extern "C", success, Dumpbin/exports Hello.dll get _fnhelloTen UI2. EXE compiled as C + + project, HELLO. DLL compiled to C project, Hello.h, no __declspec (dllexport), with DEF file: One Hello.h has extern "C", success, Dumpbin/exports Hello.dll get _fnhello A Hello.h no extern "C", success, Dumpbin/exports Hello.dll get _fnhello - UI2. EXE compiled to C project, HELLO. DLL compiled as C + + project, Hello.h, with __declspec (dllexport): - Hello.h has extern "C", success, Dumpbin/exports Hello.dll get _fnhello the Hello.h no extern "C", failed , dumpbin/exports hello.dll get [email protected] @YAXXZ - UI2. EXE compiled to C project, HELLO. DLL compiled as C + + project, Hello.h, no __declspec (dllexport), with DEF file: - Hello.h has extern "C", success, Dumpbin/exports Hello.dll get _fnhello -Hello.h no extern "C", success, Dumpbin/exports hello.dll get [email protected] @YAXXZ

Summarize:
When a DLL is implemented in C + +,
With a DEF file, without extern "C", the function name exported in the DLL will still be modified by the C + + compiler, but the function can still be found using GetProcAddress.
With __declspec (dllexport), if there is no extern "C", GetProcAddress cannot find the function.

When the DLL is a C implementation,
Because the C compiler is used, no extern "C" is required for the DEF file or __declspec (dllexport), and the function name exported in the DLL is never modified.
GetProcAddress can always find functions.

extern "C" and DEF files.

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.