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.

  • 相关阅读:
    dbt macro 说明
    构建一个dbt 数据库适配器
    dbt 包的构建
    dbt 生产环境使用
    来自官方的一些dbt 最佳实践
    knowledge-repo 知识管理简单试用
    dbt 包管理
    dbt 的知识文档管理
    knowledge 开源知识管理系统
    hasura graphql server 集成gitlab
  • 原文地址:https://www.cnblogs.com/xmphoenix/p/3973829.html
Copyright © 2011-2022 走看看