File Conversion DLL MinGW

Source: Internet
Author: User

MinGW:
C-O gcc-c a.c
C-exe GCC A.C libs.o-o a.exe (A.C from main program, additional libs, generate A.exe)
O-exe gcc a.o b.o ...-o main.exe
C-dll,def,a gcc a.c-shared-o a.dll-wl,--output-def,a.def,--out-implib,liba.a
A-DLL A2dll LIBA.A
DLL, A:dlltool--dllname a.dll--def a.def--output-lib liba.a (requires def file)
A-Def:dumpbin/exports LIB.A > Lib.def (called on Windows, Def needs to be modified)
DLL, Def:pexports A.dll-o > A.def (Here's-o refers to the function ordinal)
Lib--def:reimp-d a.lib
Lib-A: (for __cdecl functions in the most case) Reimp a.lib; (for __stdcall functions)

MSVC:
C--Lib Cl/ld a.c (note that the export list is already defined)
C-DLL Cl/ld A.C
C--obj cl/c A.C
C-EXE CL A.c/out:a.exe
DLL->lib lib/machine:ix86/def:a.def/out:a.lib (requires def file)
Obj->lib lib a.obj b.obj .../out:mylib.lib
DLL->def DUMPBIN a.dll/exports/out:a.def (generated def needs to be corrected)
Lib->def reimp-d A.lib (this is to be used under the Msys+mingw)

The scope of application of these tools can be easily understood and memorized.

Both the DLL and EXE are PE files, so you can use Pexports.

Lib and a are static library files, both archive types, not PE formats. So you can't use Pexports.

DLLs can use Dlltool.

Lib can use Lib, and Reimp (Lib->a tool)

All bin files, including Dll,exe,lib,a, can be used dumpbin.

Reference:

Http://hi.baidu.com/kaien_space/blog/item/5e77fafa2ba9ff16a8d3110a.html
MinGW Official documents: 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

The code for this test:

1. main.cpp

#include <iostream>
#include <stdlib.h>
#include "Mylib.h"

using namespace Std;

int main ()
{
Char str[]= "Hello world!";
Printhello (str);

return 0;
}

2. Mylib.cpp

#include <iostream>
#include <stdlib.h>
#include "Mylib.h"

using namespace Std;

void EXPORT Printhello (char *str)
{
cout << str << Endl;
}

3. Mylib.h


#define EXPORT __declspec (dllexport)

extern "C"
{
void EXPORT Printhello (char *str);
}

About the definition and use of DLLs:
1. Functions that require an external call, when defined, are added before the function declaration.
__declspec (dllexport)
As a convenience, you can define macros
#define EXPORT __declspec (dllexport)
It is then used when defining a function declaration, for example:
void EXPORT Printhello (char *str);

Only functions that indicate export can appear in the DLL's output table for external functions to be called.
About the function calling convention __cdecl or __stdcall. We can add it before the output function name, or it can be compiled with CL to indicate
/GD using __cdecl calling convention (C Declaration, C and C + + default format), Manual stack balancing (variable parameter support)
/GZ using the __stdcall calling convention (the calling convention for Pascal, FORTRAN, etc.), automatic stack balancing
There are other calling conventions, such as _fastcall, which pass the first two parameters through the register and call fast.

To use a DLL, we can invoke it dynamically or convert it to a Lib library static call.
Dynamic invocation means that the LoadLibrary is loaded into memory first. Then use GetProcAddress to get the function address. More trouble.
Static invocation means generating a DEF file for the function that needs to be called, and then making a static library lib file. Then use this lib to invoke the function in the DLL.

__cdecl and __stdcall

When compiling C and C + + programs on a VC, the __CDECL function calling convention is used by default. If you want to generate __stdcall functions, we can compile with/gz. For example:

Cl/gz/ld Mylib.cpp

The resulting DLL and LIB are the __stdcall conventions used

Use the following command

Dumpbin/exports Mylib.dll or mylib.lib we can see

Ordinal hint RVA name

1 0 0000107E [email protected]

The following is the __CDECL function name writing specification

Ordinal hint RVA name

1 0 0000107E Printhello

More prefixes and suffixes are visible in the __stdcall.

def file format:
LIBRARY DLLNAME. Dll
Exports
Fonctionname1 @1
Fonctionname2 @2
................

(Note the DEF file differences between the __cdecl and __stdcall calling conventions.) )


To Create a def file from a DLL:

How to implement on MinGW:

Pexports Mydll.dll-o > Mydll.def

How to implement on MSVC:

1. Make a copy of the DLL's export function table, using the VC's DUMPBIN command
DUMPBIN mydll.dll/exports > Mylib.def
2. Open def file modification
i) Add LIBRARY mydll.dll
Exports
II) Add the modified exports to the list of function names in Lib after

(Note: Because of the different function calling conventions, the exported function names will have prefixes or suffixes, which are not modified as much as possible.) Otherwise, it may not be called properly! Specific operation, the following examples will be explained)
(It is very noteworthy: this exports list of functions, and in some cases, you may not be able to know the writing rules of these function names at all.) Don't always think that the names you get in dumpbin can be generalized. Also do not think that the Def file obtained by Pexports will not be modified. In fact, we do not use extern "C" {} when declaring a function. So when you call a MinGW DLL in a VC, you'll find that the problem becomes tricky. The function name writing rules for DEF are related to the function conventions that you want to call DLLs, regardless of the prefix in the DLL. Remember! )
In addition, when using a function in a DLL, a. h file is required to declare the function that is called. It is important to note that there are no additional prefixes in the function names here.


Generate DLLs and Lib in VC (call DLL library)

Cl/ld mylib.cpp (Get mylib.dll and Mylib.lib)

generate DLLs in MinGW, Def and a (call DLL library)
g++ mylib.cpp-shared-o mylib_linux.dll-wl,--output-def,mydll.def,--OUT-IMPLIB,MYLIB.A

Generate Static Library lib in VC (no DLL used)

(Note: Static libraries (Lib and a) are actually an archive file)

Cl-c mylib.cpp compiling CPP get obj

Lib Mylib.obj/out:mylib.lib (Generate archive file mylib.lib)

generating Static Library A in MinGW (without using DLLs)
g++-c mylib.cpp get o file

AR r mylib.a mylib.o generate archive file MYLIB.A

VC calls to VC DLL (lib mode)

There are two cases of LIB file and no Lib file. The second case requires that Mr. LIB file be called again.

We are interested in the second case. The actual operation is as follows:
We only have one mylib.dll, we need to call an output function Printhello
1. Making Def
DUMPBIN Mylib.dll/exports >mylib.def
Get a complete list of output functions.
Add LIBRARY Mylib.dll at the beginning
And then put the following output function information
Ordinal hint RVA name

1 0 0000107E Printhello
Modified into
Exports
Printhello @1
All other information is deleted.

You can also call Pexports mylib.dll > Mylib.def directly on MinGW (this is the convenient place for MinGW)

2. Generate Lib
Need Mylib.dll and Mylib.def
Lib/machine:ix86/def:mylib.def
This will generate two files of Mylib.lib and Mylib.exp. (Mylib.exp can be deleted)

3. Invoking DLLs through Lib
Add # include "Mylib.h" to program Main.cpp
So you can call this function.

CL main.cpp mylib.lib compilation generates Main.exe file.
(Note: The operation of this program requires DLL participation!) After compiling lib file can be deleted, but Mylib.dll can't delete, remember! )

MinGW calling MinGW DLL (direct connection and a connection)

MinGW DLLs can be used as direct connections as static libraries. A.

g++ main.cpp Mylib.dll

You can also use

g++ Main.cpp-lmylib

Directly call DLL compilation to generate the Main.exe file. A very simple aspect.

Of course, from a research standpoint, let's discuss how to make a connection through library file a. The method and VC are identical.

Consider the absence of a file.
1. Making Def
Call Pexports mylib.dll > Mylib.def directly

2. Generate a
Need Mylib.dll and Mylib.def
Dlltool--dllname mylib.dll--def mylib.def--output-lib LIBMYLIB.A

This will generate the library file Libmylib.a file.

3. Calling a DLL through a

Add # include "Mydll.h" to program Main.cpp
So you can call the function in the DLL.

g++ main.cpp libmylib.a-o main.exe compilation generates Main.exe files.

DLL that calls MinGW in VC
Now how to call MinGW generated Mylib_linux.dll in VC? (Note: VC cannot use MinGW's a file, nor can it call DLL directly as MinGW)
We can use DEF file to generate VC available lib, call through Lib

If you do not have a def file, then use the previous method (dumpbin (manual), or Pexports (auto)) to generate one.

If you already have a def file. The contents are as follows:
LIBRARY Mylib_linux.dll
Exports
Printhello @1

The following command can generate LIB based on Def

Lib/machine:ix86/def:mydll.def generates Mydll.lib.
Invoking DLLs through Lib

CL main.cpp mydll.lib generate Main.exe Call DLL

DLL that calls the VC in MinGW

If the DLL is a __cdecl convention, then it can be used directly as a static library. If

If you are using the __stdcall calling convention. At this point, we can't use it directly like __cdecl. We have two kinds of ideas, one is to generate the Lib on the VC, and then call directly. The other is to make Def and a files through which they call the DLL.

(Note that in this case, you cannot get a from Lib with Reimp.) Even if this lib can be used directly. The generated A is also not available. You need to follow these steps to get the A file available)

1. Making Def
Call Pexports Mylib.dll | Sed "s/_//" > mylib.def (SED partial removal function name _)

Not to be continued ...

2. Generate a
Need Mylib.dll and Mylib.def
Dlltool-u-D mylib.dll-d mylib.def-l LIBMYLIB.A (Note that this-u must not be less)

This will generate the library file Libmylib.a file.

3. Calling a DLL through a

Add # include "Mydll.h" to program Main.cpp
So you can call the function in the DLL.

g++ main.cpp libmylib.a-o main.exe compilation generates Main.exe files.

Note that the function calling __stdcall must also be declared as __stdcall as follows:

extern "C"
{
void __stdcall Printhello (char *str);
}

In the VC does not need to modify the code, CL compile when using/GZ can.

Written here, basically has been discussed almost. As for the conversion between static libraries. Lib and. A. It is said that this is the same type of archive file, the difference is only the content contained in the archive. Lib contains the. obj file, and a contains an. o file. However, the format of the two files is said to be the same, but we found that Lib and a can not be universal! (Note that Static library Lib here is not the kind of static library Lib when calling DLLs.) That Lib is just the function of indexing and connecting DLLs, and here the static library is the library that works out of DLL, and the function process is included in the library. I used to write an article about libraries and MinGW common methods on Cygwin. In fact, now we can understand more clearly that they are universal because both are compiled with GCC. The result of the same compiler can of course be compatible. The value of that article lies in. Cygwin Although there are many toolkits, DLL support is required to run independently. MinGW, however, can generate programs that do not rely on DLLs to run independently.

So what exactly is the reason Lib and a cannot be generalized? At first it felt possible to be on the compiler. Because the VC compiler CL and the GCC compiler compiled the obj and O files, although the format is the same but not universal. Experiments show that the O file to cl, or to use obj to GCC can not pass. However, I do not doubt that! Because I think the most probable reason is not here. The reason for this is likely to be that two compilers have called different library functions. CL invokes the library function provided by MSVC, and GCC invokes his own library function. So we find that the general static library fails with errors such as the library function not defined. In fact, the symbol of the function name is not recognized. If you know the specific file of the library function you want and add it to the compilation of the project, it is likely that the problem will be resolved. However, the current general method of static library needs further discussion.

http://blog.csdn.net/kl222/article/details/41822561

File Conversion DLL MinGW

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.