Home > Back-end >  MSVC vs. MinGW DLL play strategy's note
MSVC vs. MinGW DLL play strategy's note

Time:11-05

Turn in beam of sina weibo
The original path: http://blog.sina.com.cn/s/blog_4f183d960100gqfj.html

A rough study records, to fill out and finishing,



MinGW:
C - & gt; O GCC -c a.c
C - & gt; Exe GCC a.c libs. O - o a.e xe (generated from the main program a.c, additional libs, a.e xe)
O - & gt; Exe GCC a.o b.o.. - the main o. Exe
C - & gt; DLL, def, a GCC a.c -shared -o a. d. ll - Wl - output - def, a. d. ef, - out - implib, liba. A
A - & gt; DLL a2dll liba. A
DLL - & gt; A: dlltool - dllname a. d. ll - def a. d. ef - output - lib liba. A (def file)
A - & gt; Def: dumpbin/exports lib. A & gt; Lib. Def (on Windows calls, def need to modify)
DLL - & gt; Def: pexports a. d. ll - o & gt; A. d. ef - o (here refers to give function standard serial number)
Lib - & gt; Def: reimp - d a. ib
Lib - & gt; A: (for __cdecl functions provides will case) reimp a. ib. (for __stdcall functions provides)

MSVC:
C - & gt; Lib cl/LD a.c (note that have defined the export list)
C - & gt; DLL cl/LD a.c
C - & gt; Obj a.c cl/c
C - & gt; Exe cl a.c/out: a.e xe
DLL - & gt; Lib lib/machine: ix86/def: a. d. ef/out: a. the ib (def file)
Obj - & gt; Lib lib a.o bj b.o bj... In/out: mylib lib
DLL - & gt; Def DUMPBIN a. d. ll/EXPORTS/OUT: a. d. ef (generated def need correction)
Lib - & gt; Def reimp - d a. ib (the) under MinGW MSYS +



About the scope of application of these tools can easily understand and remember,

DLL or exe is PE file, you can use pexports.

Lib and a static library file, file type, not PE format, so can't use pexports.

DLL can use dlltool.

The lib can use lib, and reimp (lib - & gt; A tool)

All the bin file, including the DLL, exe, lib, you can use a dumpbin.



Reference:

http://hi.baidu.com/kaien_space/blog/item/5e77fafa2ba9ff16a8d3110a.html
Mingw website document: http://www.mingw.org/wiki/MSVC_and_MinGW_DLLs
http://oldwiki.mingw.org/index.php/CreateImportLibraries
http://www.mingw.org/wiki/FAQ
http://hi.baidu.com/opaquefog/blog/item/9b21b6deb324e25dccbf1ab7.html
http://qzone.qq.com/blog/8330936-1238659272

http://hi.baidu.com/jzinfo/blog/item/b0aa1d308de99f9da8018e00.html





This test code:

1. Main. CPP

#include
#include
# include "mylib. H"

using namespace std;

Int main ()
{
Char STR []="Hello world!" ;
Printhello (STR);

return 0;
}

2. Mylib. CPP

#include
#include
# include "mylib. H"

using namespace std;

Void EXPORT printhello (char * STR)
{
cout }

3. Mylib. H


# define EXPORT __declspec (dllexport)

Extern "C"
{
Void EXPORT printhello STR (char *);
}



About the definition and use of DLL:
1. When it requires external call function, defined in the function declaration before joining
__declspec (dllexport)
Convenience, you can define ChengHong
# define EXPORT __declspec (dllexport)
Then use when defining function declarations, such as:
Void EXPORT printhello STR (char *);

Only indicate the function to appear in the output of the DLL EXPORT table, external function can invoke,
About the function calling convention __cdecl or __stdcall, we can be added before the output function, can also use cl compile time specify
/Gd use __cdecl calling conventions (C declaration, C and C + + is the default format), manual balance stack variable parameters (support)
/Gz use __stdcall calling conventions (calling convention is PASCAL, FORTRAN, etc), automatic stack balance
There are other calling conventions, such as _fastcall, the former two parameters by register transfer, call speed,

To use the DLL, we can call it dynamic, can also be converted to static lib library calls,
Dynamic invocation is, to use LoadLibrary first loaded into memory, then use GetProcAddress call function addresses to use, more troublesome,
Static call that is to say, the need to call the function to generate def file, and then make static library.lib file, then use the lib to invoke the functions in the DLL,

__cdecl and __stdcall

VC on compiling C and C + + program, use the default __cdecl function calling convention, if you want to generate __stdcall function, we can use/GZ compilation. For example:

Cl/Gz/LD mylib. CPP

This generated DLL and lib is to use __stdcall agreed

By using the following command

Dumpbin/exports mylib. DLL or mylib. Lib we can see

The ordinal hint RVA name

1 0 0000107 e _printhello @ 4

The following is a function of the __cdecl name written specifications

The ordinal hint RVA name

1 0 0000107 e printhello

Much more visible in the __stdcall prefixes and suffixes,











DEF file formats:
The LIBRARY DLLNAME. DLL
EXPORTS
Fonctionname1 @ 1
Fonctionname2 @ 2
.

(note the __cdecl and __stdcall calling convention DEF file writing,)


From the DLL def file:

Implementation method on the MinGW:

Pexports mydll. DLL - o & gt; Mydll. Def

MSVC on the implementation of the method:

1. Make a table of DLL export functions, use VC dumpbin command
Dumpbin mydll. DLL/exports & gt; Mylib. Def
2. Open the def file modification of
I) add LIBRARY mydll. DLL
EXPORTS
Ii) at the back of EXPORTS to join change good need to export to function in the lib list

(note: because the function calling convention is different, so the exported function name prefix or suffix, these all do not change as far as possible, otherwise may not be able to normal call! Behind the specific operation, will illustrate)
(it is worth noting: the EXPORTS very much a list of functions, in some cases, you may never know the writing rules of the function name, don't always think of the dumpbin the name can general, also don't think pexports def file need not change, in fact, it's no use if we declare functions extern "C" is {}. Then you call a MinGW DLL in VC you will find that problems become very tricky, def function name written rules and you have to call DLL function of the relevant agreement, has nothing to do with the former suffix in the DLL, remember!)
In addition, when using the function in the DLL, need a. H file, statement to the calling function, the function name here didn't before those additional suffix, in this note,


Generated DLL in VC and lib (call DLL library)

Cl/LD mylib. CPP (get mylib. DLL and mylib. Lib)



In MinGW generated DLL, def and a (call DLL library)
G + + mylib. CPP -shared -o mylib_linux. DLL - Wl - output - def, mydll. Def, - out - implib mylib. A






nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
Next:c
  • Related