为什么CImg能够达到这样的效果呢?

发布于 2024-09-10 16:11:55 字数 136 浏览 1 评论 0原文

编译是即时完成的: 仅真正使用了 CImg 功能 由你的程序编译并 出现在编译后的可执行文件中 程序。这导致非常紧凑 代码,没有任何未使用的东西。

有谁知道原理吗?

The compilation is done on the fly :
only CImg functionalities really used
by your program are compiled and
appear in the compiled executable
program. This leads to very compact
code, without any unused stuffs.

Any one knows the principle?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝海似她心 2024-09-17 16:11:56

CImg 是一个仅包含头文件的库,并且他们广泛使用模板,这就是他们所指的。

如果他们使用某种预编译库(.dll/.lib/.a/.so),则库文件必须包含整个 CImg 库,无论您实际使用其中的哪些部分。

对于静态链接库(.lib 或 .a),链接器可以删除未使用的符号,但这可能取决于优化设置。

当整个库包含在一个或两个头文件中时,只有当您#include它时,它才会被实际编译,因此它是与程序的其余部分相同的编译过程的一部分,并且编译器可以轻松确定库的哪些部分已使用,哪些部分未使用。

而且由于 CImg API 使用模板,因此不会为从未调用的函数生成任何代码。

不过,他们有点夸大了它,因为正如其他答案指出的那样,未使用的符号通常会被删除无论如何

CImg is a header-only library, and they use templates liberally, which is what they're referring to.

If they used a precompiled library of some kind (.dll/.lib/.a/.so) the library file would have to contain the entire CImg library, regardless of which bits of it you actually use.

In the case of a statically linked library (.lib or .a), the linker can then strip out unused symbols, but that may depend on optimization settings.

When the entire library is included in one or two headers, it is only actually compiled when you #include it, and so it is part of the same compilation process as the rest of your program, and the compiler can easily determine which parts of the library are used, and which ones aren't.

And because the CImg API uses templates, no code is generated for functions that are never called.

They are overselling it a bit though, because as the other answers point out, unused symbols will usually be stripped out anyway.

孤檠 2024-09-17 16:11:56

对我来说,这听起来像是相当标准的行为 - C++ 链接器通常会丢弃任何未使用的库引用,而不是包含不可调用的代码。同样,优化的构建不会包含不可调用的代码。

Sounds like fairly standard behaviour to me - a C++ linker will usually throw away any unused library references rather than including uncallable code. Similarly, an optimized build will not include uncallable code.

空‖城人不在 2024-09-17 16:11:56

这听起来像 MSVC 的 Eliminate Unreferenced Data (/OPT:REF) 链接器命令,GCC 也应该有类似的东西

This sounds like MSVC's Eliminate Unreferenced Data (/OPT:REF) linker command, GCC should have something similar too this too

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文