How to include libraries in Visual Studio 2012?
Asked Answered
P

2

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 :(

Prose answered 18/11, 2013 at 22:0 Comment(3)
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.Foxworth
In C and C++, you #include header files. But as @Foxworth says, there is no C++ version of this library. So you'll need to try a different library (or switch languages:)Tavey
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.Prose
S
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

Spa answered 18/11, 2013 at 22:20 Comment(7)
Thank you. I picked library that doesn't support C++. Anyway, this will be useful for me in the future. +1Prose
If step 5 doesn't work with the system32 directory, try putting it in the SysWOW64 directory.Mcauley
In Visual Studios 2013, to get to Configuration Properties: first right click on the project then go to Add->References…Duntson
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? github.com/mindboards/ev3sources/tree/master/lms2012Millar
@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.Spa
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?Ichthyology
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)Rowenarowland
S
7

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

example:

#pragma comment( lib, "yourLibrary.lib" )
Secundine answered 19/4, 2016 at 9:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.