54

I have had C++ experience but not MSVC.

What I am trying to do is incorporate a .dll from an open source project into my project. The code is available and I have built it. I have the .dll as well as the .lib which as I understand it is required for C++ projects.

Now unfortunately there is no simple "Add Reference", drop my .dll into an include directory and add that to my solution. I have edited the project property pages, the C/C++ Additional Include Directories option as well as adding the .lib as an additional linker dependency. I have created an include directory for the dll and lib inside my solution tree.

My problem is when I try to include the header files from the documentation, VS output spits out error messages. Now I realize that I am using the dll/lib combo and that the .h files are not present in my solution so how do I add the proper includes? I am using QT toolkit also which is working but how I add the other header / dll from the open source library eludes me.

Can someone please point me in the right direction.

3
  • 2
    You say "VS output spits out error messages", but we need to know what they were to fix it.
    – GManNickG
    Commented May 1, 2009 at 2:39
  • I have my .cpp and .h files in Solution/src. The .dll/lib are in Solution/include. I had added Solution/include to "Linker -> Additional Library Directories" and "C/C++ -> Additional Include Directories". The header that I need is QTwitLib.h from TwitLib library. A #include "QTwitLib.h" or even ../ or include/QTwitLib.h all give me a "fatal error: Cannot open include file: No such file". Commented May 1, 2009 at 3:05
  • Can't help but wonder what's the point of making (and approving) a trivial, inconsequential edit to a 7+ years post.
    – dxiv
    Commented Aug 4, 2016 at 5:58

3 Answers 3

85

You need to do a couple of things to use the library:

  1. Make sure that you have both the *.lib and the *.dll from the library you want to use. If you don't have the *.lib, skip #2

  2. Put a reference to the *.lib in the project. Right click the project name in the Solution Explorer and then select Configuration Properties->Linker->Input and put the name of the lib in the Additional Dependencies property.

  3. You have to make sure that VS can find the lib you just added so you have to go to the Tools menu and select Options... Then under Projects and Solutions select VC++ Directories,edit Library Directory option. From within here you can set the directory that contains your new lib by selecting the 'Library Files' in the 'Show Directories For:' drop down box. Just add the path to your lib file in the list of directories. If you dont have a lib you can omit this, but while your here you will also need to set the directory which contains your header files as well under the 'Include Files'. Do it the same way you added the lib.

After doing this you should be good to go and can use your library. If you dont have a lib file you can still use the dll by importing it yourself. During your applications startup you can explicitly load the dll by calling LoadLibrary (see: http://msdn.microsoft.com/en-us/library/ms684175(VS.85).aspx for more info)

Cheers!

EDIT

Remember to use #include < Foo.h > as opposed to #include "foo.h". The former searches the include path. The latter uses the local project files.

5
  • 32
    Perfect answer. I just wanted to add an update: in VS 2010, step 3 has been changed a bit; now you right-click your project in the Solution Explorer, choose Properties, then go to Configuration Properties > VC++ Directories.
    – Nate C-K
    Commented Jan 5, 2011 at 18:59
  • 4
    Correct me if I'm wrong, but I think we also have to make sure the .dll files can be found at runtime. An easy way to do this is to copy the .dll files to the same directory as the .exe for the solution. See here for more info.
    – littleO
    Commented Dec 27, 2015 at 10:02
  • 1
    I tried to do that in VS 2013 and I don't find the way to do step 3.
    – STF
    Commented Jan 3, 2016 at 12:37
  • 5
    Step 3 can has been invalid since VS 2010. "VC++ Directores are now available as a user property sheet that is added by default to all projects".
    – BillJam
    Commented Sep 26, 2017 at 13:08
  • using "" on include searches the local directories FIRST, then searches the same ones as <> (those specified as Include Directories, Environment Variables, etc.) Commented Aug 29, 2022 at 13:24
3

The additional include directories are relative to the project dir. This is normally the dir where your project file, *.vcproj, is located. I guess that in your case you have to add just "include" to your include and library directories.

If you want to be sure what your project dir is, you can check the value of the $(ProjectDir) macro. To do that go to "C/C++ -> Additional Include Directories", press the "..." button and in the pop-up dialog press "Macros>>".

1

You mention adding the additional include directory (C/C++|General) and additional lib dependency (Linker|Input), but have you also added the additional library directory (Linker|General)?

Including a sample error message might also help people answer the question since it's not even clear if the error is during compilation or linking.

Not the answer you're looking for? Browse other questions tagged or ask your own question.