Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2717710
  • 博文数量: 416
  • 博客积分: 10220
  • 博客等级: 上将
  • 技术积分: 4193
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-15 09:47
文章分类

全部博文(416)

文章存档

2022年(1)

2021年(1)

2020年(1)

2019年(5)

2018年(7)

2017年(6)

2016年(7)

2015年(11)

2014年(1)

2012年(5)

2011年(7)

2010年(35)

2009年(64)

2008年(48)

2007年(177)

2006年(40)

我的朋友

分类: C/C++

2010-03-23 13:43:37

#ifndef LIB_H
#define LIB_H
#ifdef LIBAPI
 #define EXPORT __declspec(dllexport)
#else
 #define EXPORT __declspec(dllimport)
#endif
extern "C"
{
 int EXPORT add(int x, int y);
}
#endif

2. testDll.cpp : Defines the entry point for the DLL application.

#include "stdafx.h"
#include
#define LIBAPI
#include "dll.h"
BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
 switch (ul_reason_for_call)
 {
  case DLL_PROCESS_ATTACH:
   printf("\nprocess attach of dll");
  break;
  case DLL_THREAD_ATTACH:
   printf("\nthread attach of dll");
  break;
  case DLL_THREAD_DETACH:
   printf("\nthread detach of dll");
  break;
  case DLL_PROCESS_DETACH:
   printf("\nprocess detach of dll");
  break;
 }
 return TRUE;
}
int EXPORT add(int x, int y)
{
 return x + y;
}

3. 用VC++生成testDll.dll, testDll.lib,并将它们与dll.h copy到gcc工作目录下

4。Test.cpp

#include
#include
using namespace std;
int main (int argc, char *argv[])
{
 cout << "hello world" << endl;
 int count = add(10, 20);
 return(0);
}

5. 编译:
g++ Test.cpp testDll.lib -o Test -I.

6。 执行:
./Test.exe

按参考文档(MinGW中调用VC的dll )做,test.exe调用的是_add,不知为什么(用depends查看)

/**************************************/
Assume   we   have   a   testdll.h,   testdll.c,   and   testmain.c.   In   the   first   case,   we   will   compile   testdll.c   with   MinGW,   and   let   the   MSVC-compiled   testmain   call   it.   You   should   use  
   
  gcc   -shared   -o   testdll.dll   testdll.c   \  
          -Wl,--output-def,testdll.def,--out-implib,libtestdll.a    
  to   produce   the   DLL   and   DEF   files.   MSVC   cannot   use   the   MinGW   library,   but   since   you   have   already   the   DEF   file   you   may   easily   produce   one   by   the   Microsoft   LIB   tool:    
   
  lib   /machine:i386   /def:testdll.def    
  Once   you   have   testdll.lib,   it   is   trivial   to   produce   the   executable   with   MSVC:  
   
  cl   testmain.c   testdll.lib    
  Now   for   MinGW   programs   calling   an   MSVC   DLL.   We   have   two   methods.   One   way   is   to   specify   the   LIB   files   directly   on   the   command   line   after   the   main   program.   For   example,   after  
   
  cl   /LD   testdll.c    
  use    
   
  gcc   -o   testmain   testmain.c   testdll.lib    
  The   other   way   is   to   produce   the   .a   files   for   GCC.   For   __cdecl   functions   (in   most   cases),   it   is   simple:   you   only   need   to   apply   the   reimp   tool   from   Anders   Norlander   (since   his   web   site   is   no   longer   available,   you   may   choose   to   download   here   a   version   enhanced   by   Jose   Fonseca):  
   
  reimp   testdll.lib  
  gcc   -o   testmain   testmain.c   -L.   -ltestdll    
  However,   for   __stdcall   functions,   the   above   method   does   not   work.   For   MSVC   will   prefix   an   underscore   to   __stdcall   functions   while   MinGW   will   not.   The   right   way   is   to   produce   the   DEF   file   using   the   pexports   tool   included   in   the   mingw-utils   package   and   filter   off   the   first   underscore   by   sed:  
   
  pexports   testdll.dll   |   sed   "s/^_//"   >   testdll.def    
  Then,   when   using   dlltool   to   produce   the   import   library,   add   `-U'   to   the   command   line:    
   
  dlltool   -U   -d   testdll.def   -l   libtestdll.a    
  And   now,   you   can   proceed   in   the   usual   way:    
   
  gcc   -o   testmain   testmain.c   -L.   -ltestdll    
  Hooray,   we   got   it.     
   

Assume   we   have   a   testdll.h,   testdll.c,   and   testmain.c.   In   the   first   case,   we   will   compile   testdll.c   with   MinGW,   and   let   the   MSVC-compiled   testmain   call   it.   You   should   use  
   
  gcc   -shared   -o   testdll.dll   testdll.c   \  
          -Wl,--output-def,testdll.def,--out-implib,libtestdll.a    
  to   produce   the   DLL   and   DEF   files.   MSVC   cannot   use   the   MinGW   library,   but   since   you   have   already   the   DEF   file   you   may   easily   produce   one   by   the   Microsoft   LIB   tool:    
   
  lib   /machine:i386   /def:testdll.def    
  Once   you   have   testdll.lib,   it   is   trivial   to   produce   the   executable   with   MSVC:  
   
  cl   testmain.c   testdll.lib    
  Now   for   MinGW   programs   calling   an   MSVC   DLL.   We   have   two   methods.   One   way   is   to   specify   the   LIB   files   directly   on   the   command   line   after   the   main   program.   For   example,   after  
   
  cl   /LD   testdll.c    
  use    
   
  gcc   -o   testmain   testmain.c   testdll.lib    
  The   other   way   is   to   produce   the   .a   files   for   GCC.   For   __cdecl   functions   (in   most   cases),   it   is   simple:   you   only   need   to   apply   the   reimp   tool   from   Anders   Norlander   (since   his   web   site   is   no   longer   available,   you   may   choose   to   download   here   a   version   enhanced   by   Jose   Fonseca):  
   
  reimp   testdll.lib  
  gcc   -o   testmain   testmain.c   -L.   -ltestdll    
  However,   for   __stdcall   functions,   the   above   method   does   not   work.   For   MSVC   will   prefix   an   underscore   to   __stdcall   functions   while   MinGW   will   not.   The   right   way   is   to   produce   the   DEF   file   using   the   pexports   tool   included   in   the   mingw-utils   package   and   filter   off   the   first   underscore   by   sed:  
   
  pexports   testdll.dll   |   sed   "s/^_//"   >   testdll.def    
  Then,   when   using   dlltool   to   produce   the   import   library,   add   `-U'   to   the   command   line:    
   
  dlltool   -U   -d   testdll.def   -l   libtestdll.a    
  And   now,   you   can   proceed   in   the   usual   way:    
   
  gcc   -o   testmain   testmain.c   -L.   -ltestdll    
  Hooray,   we   got   it.     
   

阅读(4219) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-05-04 21:26:05

http://www.mingw.org/wiki/sampleDLL