5

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.

joglidden
  • 63
  • 1
  • 1
  • 6
  • have a look at [How to add references in VS2017](https://msdn.microsoft.com/en-us/library/wkze6zky.aspx), it might help. – vikscool Sep 10 '18 at 04:02
  • @vikscool, thanks for that link. Definitely worth a read, but it doesn't refer specifically to the old static libraries and header files. But I'll give it a try. – joglidden Sep 10 '18 at 04:15

1 Answers1

8

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.

dgnuff
  • 3,195
  • 2
  • 18
  • 32
  • Ok, thanks for this advice. I'll give it a whirl and report back here. – joglidden Sep 11 '18 at 20:11
  • So, do I put the .lib file in the \library subfolder? And the .h and .dll files go in the \include folder? Obviously I missed something. Maybe it doesn't matter. – joglidden Sep 15 '18 at 04:14
  • Ok, I gave up on the C# project. I created a C++ console app, and was able to add the .h file to the project under "Header Files". Never did find a "General" tab anywhere, and therefore was not able to find an entry for "additional library directories". Sigh. Thanks for the suggestions though. – joglidden Sep 15 '18 at 04:48
  • Alright.. this is great clarification. Now I've got more gas in the tank. I'll give this a whirl and report back. – joglidden Sep 17 '18 at 01:04
  • Ok, I did everything as you described, including putting a copy of my .dll file in the \lib folder because I was getting the error `LINK : fatal error LNK1104: cannot open file 'ModelRisk_Core.dll'` because I think the linker was looking for the dll. However, after doing that, I got the error message `ModelRisk_Core.dll : fatal error LNK1107: invalid or corrupt file: cannot read at 0x2C8` So, now I'm thinking you gave me good instructions, but my dll is bad. – joglidden Sep 17 '18 at 01:57
  • It sounds like you're trying to include the `.dll` file as part of the link stage, i.e. as one of the `Additional Dependencies` on the `Input` subsection of the `Linker` section. You probably don't want to do that, you should be including the `.lib` file there. Once the `.lib` is successfully linked in, the operating system will take care of loading the `.dll` when you actually run the program. Best way to remember this: `.lib` needed to build, `.dll` needed to run. – dgnuff Sep 17 '18 at 05:43
  • 2
    _In the Linker section of project properties, also on its General tab, there's a corresponding entry for Additional library directories._ This section doesn't exist in VS 2017 15.9.14. I've been just trying to link a .lib file for the past few days, why is this so difficult? It's ridiculous. – user2445507 Jul 28 '19 at 18:18
  • @user2445507 It appears to be fixed in VS2019 16.2. Opening `Project Propertise`/ `Linker` / `General`, the first three entries are `Output File`, `Additional Dependencies` and `Additional Library Directories`. – dgnuff Jul 29 '19 at 20:02