C++ 之间的区别库(命名空间、链接)
我刚刚学习 C++,并开始使用不同的库,例如 Boost 和 SDL。在第一次努力配置路径之后,现在一切似乎都很好,但我仍然对为什么不同的库工作方式不同有一些疑问。
为什么虽然许多库(如 Boost)在其命名空间中是分开的,但还有其他库(如 SDL)却没有?对我来说,将所有内容分开更有意义,因为一个库无法知道其他库使用了哪些函数。但为什么SDL 不是这样呢?
为什么我必须为几乎每个库手动设置链接器设置(.lib 文件),而对于其他库(例如 Boost)则是自动设置?是否是因为我使用了 BoostPro 安装程序以某种方式使此链接搜索自动进行?或者还有其他我错过的设置吗?是否可以为其他库自动查找 .lib 文件?
I'm just learning C++ and started using different libraries, like Boost and SDL. After first struggling with configuring the paths, now everything seems fine, but I still have some questions about why different libraries work differently.
Why is it that while many libraries (like Boost) are separated in their namespace, there are others (like SDL) that aren't? For me, it makes more sense to keep everything separated, as one library cannot know what functions are used in other libraries. But then why is SDL not like this?
Why do I have to manually set the linker settings (.lib files), for almost every library, while it's automatic for others (like Boost)? Is it because I used the BoostPro installer what somehow made this linking search automatic? Or is there some other setting what I missed? Is is possible to make looking for .lib files automatic for other libraries?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SDL 是用 C 编写的,而 C 语言本身并不支持命名空间。 Boost 是一个纯 C++ 库,并充分利用了 C++ 命名空间。
Boost 是一个主要只有头文件的库,因此大多数时候没有实际的二进制文件可以链接。当有要链接的二进制文件(如 Boost.Thread)时,标头可能会利用特定于编译器的指令,这些指令可以命令链接器链接到某些库(如 VC++ 的
#pragma comment(lib, ...)
)。此功能在 Boost 的上下文中称为“自动链接”。否则,必须向链接器特别提及库。SDL was written in C, and the C language doesn't natively support namespaces. Boost is a C++-only library, and took full advantage of C++ namespaces.
Boost is a mostly header-only library, so there aren't actual binaries to link most of the time. When there are binaries to link (like Boost.Thread), the headers may take advantage of compiler-specific directives that can command the linker to link to certain libraries (like VC++'s
#pragma comment(lib, ...)
). This feature is called "auto link" in the context of Boost. Otherwise, libraries have to be specifically mentioned to the linker.从技术上讲,SDL 是一个 C 库。由于 C 没有命名空间,SDL 不使用它们。事实上,C++(大部分)与 C 向后兼容,这意味着您无论如何都可以在 C++ 中使用 SDL。此外,SDL 使用 C“等效”命名空间:它的所有函数都以
SDL
开头,有效地创建某种命名空间。据我所知,大多数 boost“库”不需要链接,因为它们只有标题。不过我可能是错的。
Technically, SDL is a C library. Since C doesn't have namespaces, SDL doesn't use them. The fact that C++ is (mostly) backwards-compatible with C means you can use SDL in C++ anyway. Additionally, SDL utilizes the C "equivalent" of namespaces: all its functions start with
SDL
, effectively creating some sort of namespace.AFAIK, most boost "libraries" don't require linking, as they're header-only. I could be wrong on this though.