How to manually add a library to a visual studio 2017 project?

joglidden picture joglidden · Sep 10, 2018 · Viewed 12.9k times · Source

My apologies for this very basic question that has assuredly been asked and answered before, also for my very dated terminology. I searched for an answer, but failed to find anything helpful. I am using the latest update of VS 2017 Community, and I'm trying to manually add what I used to call a "function library" to a project, which sits under a "solution".

The project is simply a C++ or C# console application. What I have is a .h (header) file, a .lib (static library) file, and a .dll (dynamic library) file. I intend to make "function calls" to this library. I'm aware that I need to have my dll in the debug folder where the executable resides, but I'm not sure how to "add dependencies" in VS 2017. I know how to manage and install NuGet packages, but these files aren't a handy-dandy NuGet package.

I hope to get advice on doing this the right (VS 2017) way.

Answer

dgnuff picture dgnuff · Sep 10, 2018

What I do in a situation like this is to create a folder, I use C:\Etc\SDKs\<name_of_library> and then within that folder create an include subfolder, and a lib subfolder. Note that the top level folder choice is completely arbitrary, place it where it makes the most sense to you.

In the C/C++ section of project properties on the General tab, or the corresponding section for C# projects, there's an entry for Additional include directories. Add the path to your newly created include folder there. That'll let you include the header file and have it work right.

In the Linker section of project properties, also on its General tab, there's a corresponding entry for Additional library directories. Add the path to your lib folder there. On the next tab down: Input there's an entry for Additional Dependencies. Add the actual name of the library file there.

Those steps should allow your project to be built using the .h, .lib and .dll files you have.

-- Edit to address comments --

The .lib file does go in the ...\lib folder, and the .h file in the ...\include, that's correct. However, you had the location of the .dll correct in your original question. That needs to be somewhere on the search path that the executable will find, so the easiest place is the same folder as the executable.

General tab is a poor choice of words on my part. General section might have been better here. When looking at the project properties, the left most pane is a tree view of the various property sections. With everything closed up, except the very top item open, you'll see

Configuration Properties
    General
    Debugging
    VC Directories
  > C/C++
  > Linker
    ...

If you then double click on C/C++ it'll open up, and show the sections specific to the C/C++ compiler:

Configuration Properties
    General
    Debugging
    VC Directories
  V C/C++
      General     <<<<<
      Optimization
      Preprocessor
      ...
  > Linker
    ...

If you click on the word `General that I've highlighted, that'll get you to the General section / tab I was talking about.

Likewise, double clicking the word Linker will open up that section, and under that you'll find the Linker General and Input sections.

Let me know if this still isn't clear, and I'll try to clarify.