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.

  • 相关阅读:
    [整理] jQuery插件开发
    windows2008r2安装笔记
    javascript 中 typeof 和 instanceof 的区别
    mysql 复制表结构和表数据
    一个例子明白 javascript 中 for 与 for in 的区别
    实现自己的框架
    Jenkins 前端项目构建报错:Vue packages version mismatch
    linux Auditd 审计工具安装报错
    linux定时压缩日志文件脚本
    Robot Framework 3.1.2 执行测试用例报错
  • 原文地址:https://www.cnblogs.com/xmphoenix/p/3973829.html
Copyright © 2011-2022 走看看