A.2 — Using libraries with Visual Studio

To recap the process needed to use a library:

Once per library:
1) Acquire the library. Download it from the website or via a package manager.
2) Install the library. Unzip it to a directory or install it via a package manager.
3) Tell the compiler where to look for the header file(s) for the library.
4) Tell the linker where to look for the library file(s) for the library.

Once per project:
5) Tell the linker which static or import library files to link.
6) #include the library’s header file(s) in your program.
7) Make sure the program know where to find any dynamic libraries being used.

Note: The examples in this lesson show screenshots from Visual Studio 2005 express, but the process hasn’t changed too substantively since then.

Steps 1 and 2 -- Acquire and install library

Download and install the library to your hard disk. See the tutorial on static and dynamic libraries for more information about this step.

Steps 3 and 4 -- Tell the compiler where to find headers and library files

We are going to do this on a global basis so the library will be available to all of our projects. Consequently, the following steps only need to be done once per library.

For Visual Studio users

The ability to do this globally has been deprecated as of Visual Studio 2013. These options are now set per project via Project Menu -> Properties. Some of the names and locations have changed, but the general idea is still the same.

A) Go to the “Tools menu” and pick “Options”.

B) Open the “Projects and Solutions” node, and click on “VC++ Directories”.

C) Under “Include Files”/”Include Directories”, add the path to the .h files for the library.

D) Under “Library Files”/”Library Directories”, add the path to the .lib files for the library.

E) Click “OK”.

Step 5 -- Tell the linker which libraries your program is using

For step 5, we need to add .lib files from the library to our project. We do this on an individual project basis. Visual Studio offers us 3 different methods for adding .lib files to our project.

A) Use a #pragma preprocessor directive to your primary .cpp file. This solution only works with Visual Studio and is non-portable. Other compilers will ignore this line.

B) Add the .lib file to your project as if it were a .cpp or .h file. This solution works with Visual Studio, but not with many other compilers. This is the solution we recommend.

C) Add the library to the linker input. This is the most “portable” solution in the sense that every IDE will provide a similar mechanism. If you ever move to another compiler or IDE, this is the solution you will have to use. This solution requires 5 steps:

C-1) In the Solution Explorer, right click on the bolded project name and choose “Properties” from the menu.

C-2) Under the “Configuration:” dropdown, select “All Configurations”.

C-3) Open the “Configuration Properties” node, the “Linker” node, and click on “Input”.

C-4) Under “Additional Dependencies”, add the name of your library.

C-5) Click “OK”.

Steps 6 and 7 -- #include header files and make sure project can find DLLs

Simply #include the header file(s) from the library in your project.

See the tutorial A1 -- Static and dynamic libraries for more information step 7.

A.3 -- Using libraries with Code::Blocks
A.1 -- Static and dynamic libraries

31 comments to A.2 — Using libraries with Visual Studio

  • George P.

    It is possible to add a 3rd party library's folders and .lib files globally to Visual Studio 2019, it just isn't as intuitive as it could be.

    Someone at CPlusPlus-dot-com explains how in a post in the forums.

    There is a follow-up reply mentioning VS 2019 has the needed option in a different place.

    I have been able to add different versions of the Boost libraries, after creating lib files using the info at the link.

  • yeokaiwei

    Dear Alex,
    Part C no longer exists in Microsoft Visual Studio 2019.
    Tools->Options->Projects and Solutions -> VC++ Directories

    C) Under “Include Files”/”Include Directories”, add the path to the .h files for the library.
    D) Under “Library Files”/”Library Directories”, add the path to the .lib files for the library.

    I think they have been changed.

  • sami


    "Steps 3 and 4 -- Tell the compiler where to find headers and library files"

    shouldn't the sentence be rephrased to the following?

    Steps 3 and 4 -- Tell the compiler and linker where to find headers and library files

  • Rom

    Images are not loading.

  • Omran

    this lesson needs an update , :)

  • sito

    just want to point out that you can't add librarys globally anymore since visual studio 2010  I believe. I've been doing research on this but i'm unable to find a way to add them globally so when this lesson gets updated it might be worth looking in to how to add them globally in visual studio 2019.

  • kushal karre

    Sir, Can I use the code of .dll / make a function call to .dll file.

    If yes, how dll is separately executed

    • Use `LoadLibrary` to load the library and `GetProcAddress` to get the address of an exported function. You need to know the function's signature, which should be part of the header the library shipped with.

  • chandra sekhar sathua

    Dear Alex

    I love your tutorials and I have been learing C++ from from a long time. You have done a great job. I want to suggest you that threre should be some tutorials on creating static and dynamic libraries. This will make this section complete.

    Chandra Sekhar Sathua

  • My dear Teacher,
    Please let me suggest you following update:

    A.2 — Using libraries with Visual Studio 2017

    General Instructions

    1) Acquire the library. Download it from the website.
    2) In directory C:\ create folder "My Libraries" and put in there the Library.
    3) In installing V.S. 2017 check the "Desktop development with C++" workload box.
    4) Create "Empty Project". Its location should be C:\My Libraries
    5) Create source file "Main.cpp".
    6) In V.S. 2017 > Solution Explorer > right click project's name > Properties.
    7) In "Property Pages" window hit C/C++ > Additional Include Directories > down arrow at the end of the field > first icon > three dots > navigate to Library and select folder (usually "include") where headers live.
    8) In "Property Pages" window hit Linker > General > Additiional Library Directories > down arrow at the end of the field > first icon > three dots > navigate to Library and select folder (usually "lib") where .lib files live.
    9) In "Property Pages" window hit Linker > Input > Additional Dependencies > the down arrow at the right of the field > Edit > copy the names of .lib files and paste in top-most text box.
    10) In "Property Pages" window hit Apply > OK.
    11) Navigate to Library where dll files live copy them and paste in project-folder.
    12) From a tutorial about this Library copy a program code and paste in Main.cpp.
    13) In Visual Studio main menu hit green arrow or press Ctrl + F5.
    Note: It's not that every Library demand all above steps. For example Graphics Library with Mathematics (GLM) only demands configure Additional Include Directories (step 7).



    With regards and friendship
    Georges Theodosiou

    • Alex

      Thanks Georges. This lesson is due for an update. I'll make sure this nice bit of feedback is incorporated when I do.

      • Dear Teacher,
        Please let me add that user can create Template (by click Project in V.S. main menu > Export Template...) with the first project, so that for new project only needs copy dll files, if any, and paste in new project-folder.
        With regards and friendship
        Georges Theodosiou

  • VS2005 Express? Now on VS2019 preview (and VS2017 Community), isn't this section a little out of date Alex?  Surely it could use an update (when you have the time of course).

    • Alex

      Definitely needs a holistic update, and is on the to-do list. In the meantime, I've updated the title, since the content should still be relatively accurate.

  • nin0

    This could definitely use an update :)

    • Alex

      It's on my list. I'll get to it... some year. :)

      • Dear Teacher, please let me suggest you begin update from title: "Using libraries with Visual Studio Community 2017". Regards.

      • Dear Teacher, please let me suggest you, when update this lesson give following links to your readers as examples of setting up libraries in Visual Studio 2017.

  • JoePerkins

    It would be nice to update this for Visual Studio Community 2015, because Tools>Options>Projects and solutions>VC++ Directories is not a valid way to set library directories anymore.

  • Hi
    I use your post but LNK2019 and LNK 2011 error happened at function ArPrintString. how can i fix errors?

  • Anurag

    I am struggling in VS10 to do same. Please provide same steps using VS2010.


  • Joe

    Great tutorials.

    What about an article on exporting functions from DLLs, rather than using a LIB file. Many DLLs seem to have undecorated (non-mangled) functions when listing them using dumpbin.exe.

    Sure you can do this using a extern 'c' directive, but that doesn't work for classes (especially with their constructors/deconstructors since they have the same name).

    Some explanation of using MOdule Definition (*.def) files would also be helpful.


  • Daniel

    Muito obrigado! Muito obrigado mesmo!!

  • m8ms

    I want to include an openSSL library
    it doesn't have a .lib file so I get confused at 5.
    when I skip it I get:
    error LNK2001: unresolved external symbol _SSL_load_error_strings

    how do I include simple .h files in VS?

    ( I included extern "C" and it didn't help)

  • srinu

    thanks a lot dude..keep it up..god bles u..!!

  • amarjit

    just perfect.....keep up the good work

  • thanks a lot...solved my problem god bless u

Leave a Comment

Put all code inside code tags: [code]your code here[/code]