如何在 C++ 中添加库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2749733/
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
How to add libraries in C++?
提问by
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++?
是的,这是一个愚蠢的问题......但是在我的两个 C++ 类中,我们根本没有这样做(除了本机库:iostream、iomanip 等)......我的问题是任何人都可以提供一个链接向 C++ 添加库的一般解释?
I do realize what what #include means; it's just I have no clue on the linker/directories in a C++ IDE.
我确实明白#include 是什么意思;只是我对 C++ IDE 中的链接器/目录一无所知。
So long question short; could I get a general explanation of terms used to link libraries in C++?
这么长的问题很短;我能得到有关 C++ 中用于链接库的术语的一般解释吗?
I'm using c::b w/ MinGW.
我正在使用 c::bw/MinGW。
采纳答案by Travis Gockel
Thiswould 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.
当您组装.cpp
..c
或任何文件时,每个翻译单元(即每个文件)都会生成一个目标文件。创建最终的可执行文件时,您将所有目标文件合并为一个二进制文件。对于静态库,您将静态存档 (.a
或.lib
) 与所有目标文件一起编译为二进制文件本身。为了链接到动态共享对象(.so
或.dll
),二进制文件是通过调用全局偏移表创建的,您通知链接器您希望与共享对象链接,操作系统加载程序在您运行程序时构建正确的映像。
A general explanation of terms used to link libraries in C++
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.
翻译- 这是由翻译单元将高级代码(在 C、Fortran 或其他语言中)翻译成汇编代码的地方。因此,每个.cpp
文件都在内部转换为特定架构的程序集。
assemble- Generates object files from the generated assembly. Object files are almostmachine code, but they have a lot of "unresolved externals," which you can kind of think of as pointers to actual function definitions.
assemble- 从生成的程序集生成目标文件。目标文件几乎是机器代码,但它们有很多“未解析的外部”,您可以将它们视为指向实际函数定义的指针。
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.
loader- 操作系统从磁盘加载二进制文件,其中包含构建程序映像所需的所有信息。虽然细节非常特定于平台,但加载器的任务通常是查找链接器生成的所有共享库引用,加载这些引用(递归,因为每个 DSO 可以有自己的依赖项)并将它们放入程序的内存空间。
回答by Travis Gockel
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:
这是一个很大的话题,我不想提供一个明确的答案。但是,由于您说您使用的是Code::Blocks,这是向您的项目添加库的顺序:
- Go to the Project menu
- Go to Build Options...
- In the options dialog, select the Linker Settings tab
- Use the Add button to select a library and add it to your project
- 进入项目菜单
- 转到构建选项...
- 在选项对话框中,选择链接器设置选项卡
- 使用“添加”按钮选择一个库并将其添加到您的项目中