c++visual-studiostatic-linkingdynamic-linkinglibharu

Why specify particular library in Visual Studio if the path to lib is known?


TL/DR: Question start with steps that I've made and that I think are important to mention (let me know if it is not important and I will delete it). The question itself is in the bottom of page. I've posted 3 questions that has the same steps that I've issued before asking the question, however questions are different. Thanks

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

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 contains files from C:\Users\wakatana\Downloads\__c++_pdf__generation\libharu-RELEASE_2_3_0\include

libharu/src contains files from C:\Users\wakatana\Downloads\__c++_pdf__generation\libharu-RELEASE_2_3_0\src

libharu/lib contains files from C:\Users\wakatana\Downloads\__c++_pdf__generation\libharu-build32\src\Debug

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

Inside main file I'm including libharu this way:

#include "libharu/include/hpdf.h"

Finally to the question:

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

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?


Solution

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

    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 - this is a program database file. It is used by a debugger.

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

    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.

    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.

    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.