C++ 如何在 Visual Studio 2012 中包含库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20058864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:53:47  来源:igfitidea点击:

How to include libraries in Visual Studio 2012?

c++visual-studio-2012

提问by Northys

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 Unirestthat can help me to get data from many APIs and after practice the basics :)

几天前我开始学习C++,我想得到一些数据,让它变得更有趣。我找到了一个名为 Unirest 的强大 C++ 库,它可以帮助我从许多 API 中获取数据,并在练习基础知识后:)

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

我不知道如何将库包含到我的项目中。我喜欢一些关于如何做到这一点的视频,所以我刚刚创建了libs文件夹(就像我在用 PHP 编程时经常做的那样)并复制了库文件。在我将头文件包含UNIRest.h到我的源代码中并将libs目录添加到项目属性 - 配置属性 - VC+ 目录中的VS+ 目录选项之后。一切都还好。但是当我打开头文件时UNIRest.h,问题出现了:

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

请你帮助我好吗?希望这不仅仅是一个愚蠢的问题,因为我试图让它整个下午都有效:(

回答by hauron

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

通常你需要做 5 件事来在你的项目中包含一个库:

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

1) 添加#include 语句所需的带有声明/接口的文件,例如:

#include "library.h"

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

2)添加一个包含目录供编译器查看

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

-> 配置属性/VC++ 目录/包含目录(单击并编辑,添加新条目)

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

3) 为 *.lib 文件添加一个库目录:

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

-> 项目(在顶部栏)/属性/配置属性/VC++ 目录/库目录(单击并编辑,添加新条目)

4) Link the lib's *.lib files

4)链接lib的*.lib文件

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

-> 配置属性/链接器/输入/附加依赖项(例如:library.lib;

5) Place *.dll files either:

5) 放置 *.dll 文件:

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

-> 在您将在Windows/system32 中打开打开最终可执行文件的目录中

回答by SridharKritha

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

同样在代码级别,您可以使用编译器指令#pragma将您的库添加到项目中。

example:

例子:

#pragma comment( lib, "yourLibrary.lib" )