How to generate DLL files by GCC in the MinGW?

32 篇文章 0 订阅

How to generate DLL files by GCC in the MinGW?

 

[Download and Install pexports]

1. Download pexports-0.43.zip.

2. Unzip pexports-0.43.zip.

$ unzip.exe pexports-0.43.zip

3. Download patchfile-1.0.zip.

4. Unzip patchfile-1.0.zip

$ unzip.exe patchfile-1.0.zip

5. Patch all related files.

$ mv patchfile-1.0 ./pexports-0.43/src/

$ cd ./pexports-0.43/src/

$ patch.exe -p0 < patchfile-1.0

6. Build and Install pexports

$ make

$ cp ./pexports.exe /bin/

 

[Edit hello.h]

  1 #ifndef _HELLO_H

  2 #define _HELLO_H

  3

  4 #if defined( __cplusplus ) || defined( c_plusplus )

  5 extern "C" {

  6 #endif

  7

  8 #ifdef BUILD_DLL

  9 #define DLL_API __declspec( dllexport )

 10 #else

 11 #define DLL_API __declspec( dllimport )

 12 #endif

 13

 14 DLL_API void MyDllSay( void );

 15

 16 #if defined( __cplusplus ) || defined( c_plusplus )

 17 }

 18 #endif

 19

 20 #endif

 

 

[Edit hello.c]

  1 #include <stdio.h>

  2 #include "hello.h"

  3

  4 void MyDllSay( void )

  5 {

  6     printf( "I am a DLL function generated by GCC in the MinGW./n" );

  7 }

 

[Build hello.c and generate DLL file]

$ gcc -c -O3 -DBUILD_DLL hello.c

$ gcc -shared -o hello.dll hello.o -Wl,--out-implib,libhello.a

Notice: The red part can not be separated by blank spaces.

 

[Build hello.lib]

1. Generate hello.def file in terms of hello.dll

$ pexports.exe hello.dll > hello.def

2. Generate hello.lib in the light of hello.def

$ lib.exe /machine:i386 /def:hello.def /out:hello.lib

 

Ok, Now, we have generated hello.dll, hello.lib, hello.exp libhello.a, so other program can use it.

 

[Summarization]

1. pexports is a very good tool to generate *.def. It can be used to generate *.lib when readers only own *.dll.

2. If readers like the simple way to build *.dll. Appendix A is an alternative by utilizing Bsymbolic option of ld.exe. Please use “ld --help” to acquire more information.

 

 [Appendix A]

If readers do not like pexports tool, gcc can automatically generate *.dll, *.def and *.a in the same time.

$ gcc -c -O3 -DBUILD_DLL hello.c

$ gcc -shared -Wl,--output-def,hello.def -Wl,--out-implib,libhello.a -Wl,-Bsymbolic -o hello.dll -Wl,hello.o

$ lib.exe /machine:i386 /def:hello.def /out:hello.lib

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值