38

I started with learning C++ a few days ago and I would like to get some data to make it more funny. I found a powerful C++ library called Unirest that can help me to get data from many APIs and after practice the basics :)

I don't know how to include libraries into my project. I fond some videos about how to do it so I just created libs folder (like i always do when I'm programming in PHP) and I copied library files. After I included header file UNIRest.h into my source and added the libs directory into VS+ Directories option in Project Properties - Configuration Properties - VC+ Directories. Everything is still OK. But when I opened the header file UNIRest.h the problem appeared:

#import "UNIHTTPRequest.h"
#import "UNIHTTPRequestWithBody.h"
#import "HttpRequest/UNISimpleRequest.h"
#import "HttpRequest/UNIBodyRequest.h"
#import "HttpResponse/UNIHTTPBinaryResponse.h"
#import "HttpResponse/UNIHTTPJsonResponse.h"
#import "HttpResponse/UNIHTTPStringResponse.h"

All of those macros are underlined and compilation failed with message:

fatal error C1083: Cannot open type library file: 'libs\unirest\unihttprequest.h': Error loading type library/DLL.

Could you please help me? Hope it's not just a stupid question because I tried to make it works whole afternoon :(

Northys
  • 1,305
  • 3
  • 16
  • 32
  • 6
    I think there's a problem here. The library you have picked is written for Objective-C which is a different language than C++. Objective-C is used mostly on the Mac, I wouldn't say for certain that it's impossible to get it working with Visual Studio (I don't know), but I think you would find it a better bet to use a different library. – john Nov 18 '13 at 22:06
  • 2
    In C and C++, you `#include` header files. But as @john says, there is no C++ version of this library. So you'll need to try a different library (or switch languages:) – crashmstr Nov 18 '13 at 22:14
  • I thought that C++ is a C with OOP support so it seemed to be the same for me, my god :D OK I gonna try to use another Library and I will change the question if it doesn't work. Do you have any library like Unirest - API calls? I'll try to use it. – Northys Nov 18 '13 at 22:14

2 Answers2

91

Typically you need to do 5 things to include a library in your project:

1) Add #include statements necessary files with declarations/interfaces, e.g.:

#include "library.h"

2) Add an include directory for the compiler to look into

-> Configuration Properties/VC++ Directories/Include Directories (click and edit, add a new entry)

3) Add a library directory for *.lib files:

-> project(on top bar)/properties/Configuration Properties/VC++ Directories/Library Directories (click and edit, add a new entry)

4) Link the lib's *.lib files

-> Configuration Properties/Linker/Input/Additional Dependencies (e.g.: library.lib;

5) Place *.dll files either:

-> in the directory you'll be opening your final executable from or into Windows/system32

user3044482
  • 401
  • 4
  • 11
hauron
  • 4,550
  • 5
  • 35
  • 52
  • 1
    Thank you. I picked library that doesn't support C++. Anyway, this will be useful for me in the future. +1 – Northys Nov 18 '13 at 22:23
  • 2
    If step 5 doesn't work with the system32 directory, try putting it in the SysWOW64 directory. – user886079 Feb 20 '15 at 19:04
  • 2
    In Visual Studios 2013, to get to Configuration Properties: first right click on the project then go to Add->References… – Joe Aug 10 '15 at 20:07
  • How would I go about including a whole number of files like this? I've downloaded this directory and would like to add dependencies for all of them? https://github.com/mindboards/ev3sources/tree/master/lms2012 – Karoh Dec 23 '15 at 00:21
  • @Horak: These seem to be just source files, so best bet would to be to first create a Project (or a Solution with multiple Projects) and compile them into libraries, and then simply follow the instructions above. Another approach is to create a Solution with both the libraries and the final Project, then use the Solution properties to configure dependencies simply. – hauron Dec 24 '15 at 08:59
  • Hi, I generate a vs project with `qmake -tp vc` from QT with some additional dependencies, but I find no libs names in `Additional Dependencies` of the vs project, but the vs project do run well. Does that mean there is some other methods to add `Additional Dependencies` in vs project? – Summer Sun Sep 19 '17 at 02:02
  • When editing VS project properties make sure that you have the right configuration and platform selected (or use "all" for both, making sure the changes have an effect, if you're not using more than one configuration). This oversight just cost me hours of my life... (including looking into the .vcxproj files and trying to edit them manually, as well as asking myself why their contents don't match the project properties shown by VS) – Adomas Baliuka Dec 05 '19 at 02:31
7

In code level also, you could add your lib to the project using the compiler directives #pragma.

example:

#pragma comment( lib, "yourLibrary.lib" )
SridharKritha
  • 8,481
  • 2
  • 52
  • 43