如果知道lib的路径,为什么要在Visual Studio中指定特定的库? [英] Why specify particular library in Visual Studio if the path to lib is known?

查看:200
本文介绍了如果知道lib的路径,为什么要在Visual Studio中指定特定的库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL/DR:问题从我已经制定的步骤开始,我认为有必要提及(请告诉我是否不重要,我将其删除).问题本身在页面底部.我已经发布了3个问题,它们的提问步骤与提出问题之前的步骤相同,但是问题有所不同.谢谢

我已经使用cmake成功编译了 libharu 库,因此我可以在自己的c ++项目中使用它来生成PDF文件.它是在Visual Studio 2013中作为Debug/Win32编译的. cmake-gui的设置方法如下:

I've successfully compiled libharu library with cmake so I can use it in my own c++ project to generate PDF files. It was compiled with Visual Studio 2013 as Debug/Win32. cmake-gui was set following way:

Sources:
c:\Users\wakatana\Downloads\__c++_pdf__generation\libharu-RELEASE_2_3_0

Build:
C:\Users\wakatana\Downloads\__c++_pdf__generation\libharu-build32

我在使用 libharu 的当前项目(从头开始创建)具有以下结构(同样是Visual Studio 2013) ):

My current project (created from scratch) that I'm using libharu in has following structure (again Visual Studio 2013):

./Debug
./libharu_example
./libharu_example/Debug
./libharu_example/Debug/libharu_example.tlog
./libharu_example/libharu
./libharu_example/libharu/include
./libharu_example/libharu/lib
./libharu_example/libharu/src
./libharu_example/libharu/src/CMakeFiles

libharu/include包含C:\Users\wakatana\Downloads\__c++_pdf__generation\libharu-RELEASE_2_3_0\include

libharu/src包含来自C:\Users\wakatana\Downloads\__c++_pdf__generation\libharu-RELEASE_2_3_0\src

libharu/lib包含C:\Users\wakatana\Downloads\__c++_pdf__generation\libharu-build32\src\Debug

Visual Studio项目设置如下:

Visual studio project settings are following:

# C/C++ -> General -> Additional Include Directories:
$(ProjectDir)libharu\include

# C/C++ -> Preprocessor -> Preprocessor Definitions:
_CRT_SECURE_NO_WARNINGS

# Linker -> General -> Additional Library Directories
$(ProjectDir)libharu\lib

# Linker -> Input -> Additional Dependencies
libhpdfd.lib

在主文件中,我这样包含 libharu :

Inside main file I'm including libharu this way:

#include "libharu/include/hpdf.h"

最后一个问题:

C:\Users\wakatana\Downloads\__c++_pdf__generation\libharu-build32\src\Debug目录中也有那些文件:

In the C:\Users\wakatana\Downloads\__c++_pdf__generation\libharu-build32\src\Debug directory there are also those files:

libhpdfd.dll
libhpdfd.exp
libhpdfd.ilk
libhpdfd.lib
libhpdfd.pdb
libhpdfsd.lib

我尝试将Linker -> Input -> Additional Dependencies也设置为libhpdfsd.liblibhpdfd.dll,但是唯一起作用的是libhpdfd.lib.上面提到的其他文件的目的是什么?如何知道应该使用哪个* .lib * .dll?另外,为什么我需要为Visual Studio指定它?它不够聪明,可以自动加载吗?它已经指定了$(ProjectDir)libharu\lib所有这些库的存储位置,为什么不仅仅自动选择最好的库呢?

I've tried to set Linker -> Input -> Additional Dependencies also to libhpdfsd.lib and libhpdfd.dll but the only one that worked was libhpdfd.lib. What is purpose of other above mentioned files and how do I know which *.lib *.dll should I use? Also why I need to specify this to Visual Studio? Isn't it smart enough that it can load it automatically? It has already specified $(ProjectDir)libharu\lib where all those libs were stored, why not just pick the best one automatically?

推荐答案

libhpdfsd.lib-这是一个静态库.静态库在构建时由链接器链接.

libhpdfsd.lib - this is a static library. Static libraries are linked at build time by the linker.

libhpdfd.dll-这是一个动态链接的库.与静态库相比,它在构建时未链接.而是使用

libhpdfd.dll - this is a dynamically linked library. In contrast to a static library, it is not linked at build time. Instead, it is loaded into process memory explicitly at runtime with LoadLibrary and addresses of its exported functions and variables are obtained with GetProcAddress. This requires writing some boilerplate code. To avoid doing that, there's often a corresponding static library, called import library, that automagically does this for you. This is what libhpdfd.lib is.

libhpdfd.pdb-这是一个程序数据库文件.由调试器使用.

libhpdfd.pdb - this is a program database file. It is used by a debugger.

libhpdfd.exp-这是一个导出文件.当您具有循环依赖性时,这很有用.

libhpdfd.exp - this is an export file. It is useful when you have cyclic dependencies.

libhpdfd.ilk-此文件用于增量链接.增量链接可以加快链接阶段,这在调试过程中对代码进行少量更改并重建所有项目时非常有用.

libhpdfd.ilk - this file is for incremental linking. Incremental linking speeds up linking phase which is useful when during debugging you make small changes in your code and rebuild all project.

您需要显式指定要使用的库,因为您可能具有不同的库或同一库的不同版本,这些库会导出具有相同名称的符号.在这种情况下,链接器无法知道应该从哪个库导入您的符号,并且会出现链接器错误.我相信将libhpdfsd.lib添加到其他依赖项时遇到的错误是因为这个原因.

You need to specify explicitly a library you use because you may have different libraries or different versions of the same library, which export symbols with the same name. In that case a linker can't know from which library your symbol should be imported and you'd get a linker error. I believe the error you got when you added libhpdfsd.lib to additional dependencies was because of this.

使用静态库还是dll由您决定.您的默认选择是使用静态库,必要时使用dll.

Whether to use the static library or the dll is up to you. Your default choice is to use the static library, use dll when you have to.

这篇关于如果知道lib的路径,为什么要在Visual Studio中指定特定的库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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