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.