C ++:链接库和添加包含目录之间的区别 [英] C++ : Difference between linking library and adding include directories

查看:183
本文介绍了C ++:链接库和添加包含目录之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很多标题总结起来。

如果我想使用库,我不能确定两者之间的区别。

I'm not sure the difference between the two if i'd like to use a library.

感谢!

推荐答案

一般来说,您需要两者。

In general, you need both.

包含文件包含类型,函数原型 inline 函数,定义 s,...,一般来说,编译器在编译文件时需要知道库的每个信息。

Include files contain declarations of types, prototypes of functions, inline functions, #defines, ..., in general every information about the library the compiler needs to be aware of when compiling your files.

静态库,而是包含库函数的实际目标代码。如果标头包含原型,则静态库包含函数的(编译的)定义,即链接器将与您的链接的对象模块。

Static libraries, instead, contain the actual object code of the functions of the library. If the headers contain the prototypes, the static libraries contain the (compiled) definitions of the functions, i.e. the object modules that the linker will link with yours.

如果你只包含头文件而不链接静态库,链接器会抱怨缺少定义,因为你将使用头文件中声明的函数 ,但不是在任何地方定义(=没有实施)。另一方面,如果你只是链接静态库而不提供头,编译器会抱怨未知的标识符,因为它不会有一个关于你正在使用的库符号的线索。

If you only included the header file without linking against the static library, the linker would complain about missing definitions, because you would be using functions declared in the header, but not defined anywhere (=with no implementation). On the other hand, if you only linked the static library without providing the header, the compiler would complain about unknown identifiers, since it wouldn't have a clue about the library symbols you're using.

这个概念与编译多文件项目非常相似:访问其他 .cpp 中写的定义,你需要包含一个头部和它们的声明,并且链接器在末端将各种对象模块链接在一起。

The concept is very similar to when you compile a multi-file project: to access the definitions wrote in other .cpp you need to include just a header with their declarations, and the linker in the end links together the various object modules.

对于dll,通常是一个 import library ;导入库就像静态库,但是,它们不包含库的所有代码,而是包含将函数调用到dll中的小存根。每次在一个对象模块中遇到对库函数的调用时,链接器会将其指向stub,stub会将其重定向到dll 1 的代码。总而言之,在Windows上处理dll时,通常有一个 .h (prototypes / ...), .lib (链接的导入库,包含存根)和 .dll (包含库的实际代码的动态链接库)。

As far as dlls are concerned, usually an import library is provided; import libraries are like static libraries, but, instead of containing all the code of the library, they contain small stubs that call the functions into the dll. Every time a call to a library function is encountered in one of your object modules, the linker directs it to the stub, which in turn redirects it to the code into the dll1. All in all, when dealing with dlls on Windows you usually have a .h (prototypes/...), a .lib (import library you link against, contains the stubs) and a .dll (dynamic-linking library containing the actual code of the library).

顺便说一下,有些库是header only(你可以在boost中找到很多),这意味着它们的所有代码都放在一个头中,因此不需要静态库。这样的库通常只是由内联代码(functions / classes / ...)和模板,因为它们不需要单独的定义。

By the way, some libraries are "header only" (you can find many in boost), which means that all their code is put into a header, so no static library is needed. Such libraries are often just made of inline code (functions/classes/...) and templates, for which no separate definition is needed.

通常这样做是因为static图书馆是丑陋的野兽,原因如下:

Often this is done because static libraries are ugly beasts for several reasons:


  • 您必须明确地与他们连结;

  • 它们直接链接到您的代码,他们必须使用完全相同的C / C ++运行时库,这意味着,至少在Windows上,分发静态库(不同的编译器,不同的编译器版本,同一编译器的不同配置是不切实际的使用不同的标准库,为这些方面的每个组合分发静态库至少是不切实际的);

  • 因为这一点,一般来说你必须先<编译您自己的版本的静态库,然后链接到它。

比较所有这一切,只包括一个头文件.. 。)

Compare all this with just including a header file... :)


  1. 实际上,现代工具链可以识别这些存根并避免额外的间接步骤。有关详情,请参阅Raymond Chen的本系列

  1. Actually, modern toolchains can recognize these stubs and avoid the extra indirection step. See this series by Raymond Chen for details.

这篇关于C ++:链接库和添加包含目录之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆