可以使用Cygwin下的gcc / g ++链接到Windows .dll + .lib文件组合吗? [英] Is it possible to link against a Windows .dll+.lib file combination using gcc/g++ under Cygwin?

查看:781
本文介绍了可以使用Cygwin下的gcc / g ++链接到Windows .dll + .lib文件组合吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何在Unix-ish上下文中与图书馆链接:如果我正在使用 .a .so 文件,我指定根搜索目录与 -L / my / path / to / lib / 和libMylib我添加 -lMyLib



但是如果我有




  • a dll (例如在 Windows \System32 目录中)?

  • a .dll (在 Windows\System32 )和一个 .lib (其他地方) ?



这些DLL是由其他方;我没有访问他们的来源 - 但是可以访问相应的包含文件,我可以编译它。

解决方案

p>如果您可以链接到Cygwin或MinGW中的 .lib ,则可以(间接)链接到DLL。



在MSVC世界中,与DLL一起创建导入库并不常见。它是加载DLL并包装DLL接口的静态库( .lib )。您只需在(静态)导入库中调用wrapper函数,并使导入库执行与DLL相关的所有操作。




  • 对于Windows API,WindowsSDK中有导入库。

  • 对于您自己的MSVC DLL,MSVC可以在构建DLL时自动生成导入库。

  • 对于第三方DLL,您可以根据相应的头文件构建静态包装库。






.lib 文件链接Cygwin或MinGW是可能的。示例:

  g ++ -o myprg myprg.o -lShlwapi 

这与Shlwapi.lib的链接。 (该库必须位于链接器的本地目录或库路径中。)



链接到DLL的导入库的工作方式相同。



注1:记住不同的ABI和名称管理。但是,在大多数情况下调用简单的C函数在DLL或LIB文件中都可以工作。



注2:请注意,g ++要求以正确的顺序指定库。


I know how to link against libraries in Unix-ish contexts: If I'm working with .a or .so files, I specify the root search directory with -L/my/path/to/lib/ and for libMylib I add -lMyLib.

But what if I have

  • a .dll (e.g. in the Windows\System32 directory)?
  • a .dll (in Windows\System32) and a .lib (someplace else)?

These DLLs are by some other party; I don't have access to their sources - but do have access to the corresponding include files, against which I manage to compile.

解决方案

If you can link against a .lib in Cygwin or MinGW, then you can (indirectly) link against a DLL.

In the MSVC world, it is not unusual to create an import library along with a DLL. It is a static library (.lib) that loads the DLL and wraps the interface of the DLL. You just call the wrapper functions in the (static) import library and let the import library do all the DLL-related things.

  • For the Windows API, there are import libraries in the WindowsSDK.
  • For your own MSVC DLLs, MSVC can automatically generate the import libraries when you build the DLL.
  • For a third party DLL, you can build a static wrapper library based on the corresponding header files.

Linking against the .lib file in Cygwin or MinGW is possible. Example:

g++ -o myprg myprg.o -lShlwapi

This links against Shlwapi.lib. (The library must be in the local directory or in the library path of the linker.)

Linking against import libraries of DLLs works the same way.

Note 1: Keep in mind the different ABIs and name mangeling. However, calling plain C functions in DLL or LIB files will work in most cases.

Note 2: Keep in mind that g++ requires the libraries to be specified in the correct order.

这篇关于可以使用Cygwin下的gcc / g ++链接到Windows .dll + .lib文件组合吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆