Visual Studio中静态和动态连接 [英] Static and Dynamic linking in Visual studio

查看:624
本文介绍了Visual Studio中静态和动态连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解静态和动态链接的概念。已知在Windows平台上, .dll 是动态库, .lib 是静态库。



我的混乱:我做了一个项目,我不得不使用OpenCV库。基本上,我不得不使用OpenCV的以下5个库:

  -lopencv_core 
-lopencv_highgui
- lopencv_imgcodecs
-lopencv_imgproc
-lopencv_videoio

为此,在该项目,我不得不告诉编译器在VS 2012的附加库目录中的库的路径,我也不得不告诉链接器关于我要用于项目的 .lib 库。该项目编译没有任何错误。但是当我尝试运行该项目时,它表示 videoio.dll 缺少(对于其余的库也是同样的错误)。一旦在 .exe 存在的文件夹中复制 .dll 文件,则该项目运行正常。



问题:为什么我已经复制了 .dll 链接静态库( .lib )?



进一步问题:当我使用在Mac OS或Linux上的Eclipse,我只需要告诉编译器OpenCV库所在的位置,以及其他OpenCV库的链接器。在这种情况下,我不必将动态库放到 .exe 文件夹中。

解决方案

通常窗口工具链提供了.lib的两个风格。



其中一个是您提到的静态库。当使用时,没有关联的.dll。



另一个是一个剥离的库,只是挂在代码中来加载和修复动态库的函数指针在加载时。



使用动态库(.dll),您可以加载自己的自己(通过。 LoadLibrary ),或者您可以使用.dll提供的存根(import).lib。喜欢导入.lib如果提供了。


为什么当我已经链接静态库时,我必须复制.dll文件.lib)?


.dll需要在路径中,或者是.exe的目录,装载器找到它。







如何区分.lib文件是静态库还是动态库?


通常,文档应包含一些关于此的详细信息。如果它们集中在同一个文件夹中,那么我假设.lib与.dll绑定。如果一切都失败,查看文件大小;如果.lib与.dll绑定,那么与.dll相比通常很小。


I understand the concept of Static and Dynamic linking. It is known that on Windows platform, .dll are the dynamic libraries and .lib are the static libraries.

My Confusion: I made a project in which I had to use the OpenCV libraries. Basically, I had to use the following 5 libraries of OpenCV:

-lopencv_core
-lopencv_highgui
-lopencv_imgcodecs
-lopencv_imgproc
-lopencv_videoio

For this purpose, in the properties of the project, I had to tell the compiler the path of the libraries in the Additional Library Directory of VS 2012 and I also had to tell linker about the .lib libraries which I want to use for the project. The project got compiled without any error. But when I try to ran the project, it said that videoio.dll is missing (same error for rest of the libraries too). As soon as I copied the .dll files inside the folder where the .exe was present, the project ran fine.

Question: Why did I have to copy the .dll files when I already linked the static libraries (.lib)?

Further Question: When I use Eclipse on Mac OS or Linux, I just have to tell the complier where the OpenCV libraries are present and to the linker which other OpenCV libraries I want to use. I never had to put the dynamic libraries to the .exe folder in that case.

解决方案

The "usual" windows tool chains offer two "flavours" of .lib.

One is the static libraries you mention. When used, there is no associated .dll.

The other is a stripped library that is only there to hook in the code to load and fix functions pointers for the dynamic library at load time.

With dynamic libraries (the .dll) you can either load it your self (via. LoadLibrary) or you can use the stub (import) .lib you have been provided with the .dll. Favour the import .lib if one is provided.

Why did I had to copy the .dll files when I already linked the static libraries (.lib) ?

The .dll needs to be in the path, or the directory with the .exe for the loader to find it.


How do I differentiate whether the .lib file is Static library or Dynamic library?

Generally the documentation should include some level of detail about this. If they are lumped in the same folder, then I would assume the .lib is tied to the .dll. If all else fails, look at the file size; if the .lib is tied to the .dll, then it is normally small in comparison to the .dll.

这篇关于Visual Studio中静态和动态连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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