如何在C++中添加库?
是的,这是一个愚蠢的问题...但是在我的两个 C++ 类中,我们根本没有这样做(本机库除外:iostream、iomanip 等)...我的问题是任何人都可以提供一个链接来提供向 C++ 添加库的一般解释?
我确实意识到#include 的含义;只是我对 C++ IDE 中的链接器/目录一无所知。
这么长的问题就这么短;我可以获得 C++ 中用于链接库的术语的一般解释吗?
我正在使用 c::bw/MinGW。
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++?
I do realize what what #include means; it's just I have no clue on the linker/directories in a C++ IDE.
So long question short; could I get a general explanation of terms used to link libraries in C++?
I'm using c::b w/ MinGW.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这您可能会感兴趣,但这里有一个简短的版本:
当您汇编
.cpp
、.c
或其他文件时,每个翻译单元(即每个文件)都会生成一个目标文件。创建最终的可执行文件时,您可以将所有目标文件合并到一个二进制文件中。对于静态库,您可以将静态存档(.a
或.lib
)与所有目标文件一起编译到二进制文件本身中。为了链接到动态共享对象(.so
或.dll
),二进制文件是通过调用全局偏移表创建的,并且您通知链接器您希望链接当您运行程序时,共享对象和操作系统加载程序会构建正确的映像。用于在 C++ 中链接库的术语的一般解释
从...开始
翻译 - 这是高级代码(C、Fortran 或其他语言)由翻译单元翻译为汇编代码的地方。因此,每个
.cpp
文件都会在内部转换为特定架构的程序集。assemble - 从生成的程序集中生成目标文件。对象文件几乎是机器代码,但它们有很多“未解析的外部”,您可以将其视为指向实际函数定义的指针。
链接 - 这会将所有目标文件放入一个连贯的二进制文件中,无论是动态共享对象还是可执行文件。您需要告诉链接器应该在哪里找到上一阶段中所有未解析的外部文件,否则它们将在此处显示为错误。
现在二进制文件位于磁盘上,等待直到...
loader - 操作系统从磁盘加载二进制文件,其中包含构建程序映像所需的所有信息。虽然细节与平台相关,但加载程序通常负责查找链接器生成的所有共享库引用,加载这些引用(递归地,因为每个 DSO 可以有自己的依赖项)并将它们放入程序的内存空间中。
This would probably interest you, but here is a short version:
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.A general explanation of terms used to link libraries in C++
Starting with...
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.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 - 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.
这是一个很大的话题,我不想提供明确的答案。但是,由于您说您正在使用 Code::Blocks,因此这是向项目添加库的顺序:
This is a huge topic, and one I don't feel like providing a definitive answer to. However, since you say you are using Code::Blocks, this is the sequence to add a library to your project: