简体   繁体   English

如何在MSVC中静态链接库?

[英]How to statically link a library in MSVC?

I am compiling my program in Microsoft Visual C++ 10, and it uses the PDCurses library. 我正在Microsoft Visual C ++ 10中编译程序,它使用了PDCurses库。 I have configured it to link against pdcurses.lib but when I try to run the application the system complains that it can't find "pdcurses.dll". 我已经配置它链接到pdcurses.lib,但是当我尝试运行该应用程序时,系统抱怨它找不到“ pdcurses.dll”。 It's not supposed to complain about it. 不应抱怨它。 I used to compile programs with MinGW (GCC) and it doesn't ask me about any DLL when I try to run the application. 我曾经使用MinGW(GCC)来编译程序,当我尝试运行该应用程序时,它不会询问任何DLL。 What can I do so that MSVC statically links PDCurses? 我该怎么做才能使MSVC静态链接PDCurses?

In MSVC .lib files can take two forms: 在MSVC中, .lib文件可以采用两种形式:

  1. a static library 静态库
  2. an import library 进口图书馆

The former can be used to make your application aware of the exported entry points from a DLL, which will then be written to the import directory of your PE file (or into another similar location for delay-loaded imports). 前者可用于使您的应用程序知道从DLL导出的入口点,然后将其写入PE文件的导入目录(或写入延迟加载的另一个相似位置)。

The latter, however, will actually link the code it contains for every referenced function into your final binary. 但是,后者实际上会将每个引用函数所包含的代码链接到最终的二进制文件中。 This is what you want, but you may have to build that static library yourself from the source code, if it is not provided by the release of the library that you want to link. 这就是您想要的,但是,如果您要链接的库的发行版未提供该静态库,则可能必须根据源代码自己构建该静态库。

However, there is another possibility: you could simply be passing the wrong directory for the linker to find the .lib files. 但是,还有另一种可能性:您可能只是将错误的目录传递给链接程序以找到.lib文件。 In this case you'd be linking against the import library instead of the static library. 在这种情况下,您将链接到导入库而不是静态库。 Make sure you check your build log for the paths used. 确保检查构建日志中使用的路径。 This, however, only applies if the distribution of the library contains both types of libraries. 但是,仅当库的发行版包含两种类型的库时才适用。

dll必须在路径中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM