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 |