如何在C ++中添加库? [英] How to add libraries in C++?

查看:190
本文介绍了如何在C ++中添加库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,这是一个愚蠢的问题...但是在我的两个C ++类,我们没有这样做(除了本机库:iostream,iomanip等)...我的问题是,任何人都可以提供链接给出了向C ++添加库的一般解释?

Yea this is a dumb question... However in both of my C++ classes we did not do this at all (except for native libraries: iostream, iomanip, etc.)... My question is can anyone provide a link that gives the general explanation of adding libraries to C++?

我意识到#include意味着什么;它只是我对C ++ IDE中的链接器/目录没有线索。

I do realize what what #include means; it's just I have no clue on the linker/directories in a C++ IDE.

这么长的问题短;我可以得到关于用于链接C ++中的库的术语的一般解释吗?

So long question short; could I get a general explanation of terms used to link libraries in C++?

我使用c :: bw / MinGW。

I'm using c::b w/ MinGW.

推荐答案

当你组装 .cpp .c 或任何文件,每个翻译单元(即每个文件)生成一个目标文件。创建最终可执行文件时,将所有目标文件合并为一个二进制文件。对于静态库,将静态归档( .a .lib )与所有目标文件一起编译到二进制本身。对于链接到动态共享对象( .so .dll ),二进制是通过调用全局偏移表,并通知链接器您希望链接到共享对象和操作系统加载器在运行程序时构建正确的映像。

When you assemble the .cpp, .c or whatever files, each translation unit (that is, each file) generates an object file. When creating the final executable, you combine all the object files into a single binary. For static libraries, you compile the static archive (.a or .lib) along with all the object files into the binary itself. For linking to dynamic shared objects (.so or .dll), the binary is created with calls to the global offset table and you inform the linker that you wish to link with the shared object and the operating system loader builds the proper image when you run the program.

从<...>开始

翻译这是高级代码(在C,Fortran或任何)通过翻译单元转换为汇编代码的地方。因此,每个 .cpp 文件在内部被翻译为特定架构的程序集。

translation - This is where the high-level code (in C, Fortran or whatever) is translated into assembly code by translation unit. So, every .cpp file is internally translated to assembly for a specific architecture.

strong> - 从生成的程序集生成目标文件。对象文件是几乎的机器代码,但它们有很多未解析的外部,你可以把它看作是指向实际函数定义的指针。

assemble - Generates object files from the generated assembly. Object files are almost machine code, but they have a lot of "unresolved externals," which you can kind of think of as pointers to actual function definitions.

链接 - 这会占用所有对象文件,并将它们放入一个连贯的二进制文件,无论是动态共享对象还是可执行文件。你需要告诉链接器在哪里应该找到所有那些未解决的外部从上一个阶段或他们将显示为错误在这里。

linking - This takes all your object files and puts them into a coherent binary, be it a dynamic shared object or an executable. You need to tell the linker where it should find all those unresolved externals from the previous stage or they will show up as errors here.

现在二进制坐在磁盘上正在等待...

Now the binary sits on disk, which is waits until...

loader - 操作系统从磁盘加载二进制文件,其中包含构建程序映像。虽然细节是特定于平台的,但是加载器通常负责找到链接器生成的所有共享库引用,加载它们(递归地,因为每个DSO可以具有它自己的依赖)并将它们放入程序的存储器空间。

loader - The operating system loads a binary off of the disk, which contains all the information needed to build the program image. While the details are extremely platform specific, the loader is generally tasked with finding all the shared library references generated by the linker, loading those (recursively, since each DSO can have its own dependencies) and putting them into the memory space of the program.

这篇关于如何在C ++中添加库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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