zoukankan      html  css  js  c++  java
  • MSVC and MinGW DLLs

    Posted February 26th, 2009 by earnie

    TODO: Reformat to new wiki syntax.

    !!! [Minimalist GNU for Windows | http://www.mingw.org]

    !! MSVC and MinGW DLLs

    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
    [this version|http://wyw.dcweb.cn/download.asp?path=&file=reimp_new.zip]
    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.

    This guide may also be helpful.
    Here is an example of creating a .lib file from a mingw dll, in order to be able to use it from MSVC.

  • 相关阅读:
    linux学习方法之一
    HDU 1556 Color the ball
    Object-c学习之路十(NSNumber&NSValue)
    蜂鸣器驱动方式源程序--有源无源通用
    Wordpress更换主题之后出错
    mybatis_Generator配置
    Logistic Regression
    求两个字符串的最大公共字串
    数据结构排序系列详解之二 希尔排序
    《mysql必知必会》学习_第五章
  • 原文地址:https://www.cnblogs.com/xmphoenix/p/3973829.html
Copyright © 2011-2022 走看看