有没有办法从我不使用的目标文件中删除所有函数?

发布于 2024-08-29 03:54:17 字数 102 浏览 7 评论 0原文

我试图节省可执行文件中的空间,并且注意到有几个函数被添加到我的目标文件中,即使我从未调用它们(代码来自库)。

有没有办法告诉 gcc 自动删除这些函数或者我需要手动删除它们?

I am trying to save space in my executable and I noticed that several functions are being added into my object files, even though I never call them (the code is from a library).

Is there a way to tell gcc to remove these functions automatically or do I need to remove them manually?

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

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

发布评论

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

评论(5

看春风乍起 2024-09-05 03:54:17

如果您正在编译成目标文件(不是可执行文件),那么编译器将永远不会删除任何非静态函数,因为您总是有可能将目标文件链接到将调用该函数的另一个目标文件。因此,您的第一步应该是声明尽可能多的静态函数。

其次,编译器删除任何未使用的函数的唯一方法是静态链接您的可执行文件。在这种情况下,至少有可能出现一个程序并找出使用了哪些函数以及未使用哪些函数。

问题是,我不相信 gcc 实际上会进行这种类型的跨模块优化。最好的选择是使用 -Os 标志来优化代码大小,但即便如此,如果您有一个目标文件 abc.o,其中包含一些未使用的非静态函数,并且您静态链接到某些可执行文件.exe,我不相信 gcc 会删除未使用函数的代码。

如果您确实迫切需要完成此操作,我认为您可能必须将这些文件实际 #include 放在一起,以便在预处理器通过后,会生成一个被编译的 .c 文件。通过 gcc 编译单个巨大的巨型源文件,您最有可能消除未使用的函数。

If you are compiling into object files (not executables), then a compiler will never remove any non-static functions, since it's always possible you will link the object file against another object file that will call that function. So your first step should be declaring as many functions as possible static.

Secondly, the only way for a compiler to remove any unused functions would be to statically link your executable. In that case, there is at least the possibility that a program might come along and figure out what functions are used and which ones are not used.

The catch is, I don't believe that gcc actually does this type of cross-module optimization. Your best bet is the -Os flag to optimize for code size, but even then, if you have an object file abc.o which has some unused non-static functions and you link statically against some executable def.exe, I don't believe that gcc will go and strip out the code for the unused functions.

If you truly desperately need this to be done, I think you might have to actually #include the files together so that after the preprocessor pass, it results in a single .c file being compiled. With gcc compiling a single monstrous jumbo source file, you stand the best chance of unused functions being eliminated.

你不是我要的菜∠ 2024-09-05 03:54:17

您是否考虑过使用 -Os 调用 gcc(优化大小)。我不确定它是否会删除未到达的代码,但它足够简单以进行测试。您还可以在取回可执行文件后,“剥离”它。我确信有一个 gcc 命令行参数可以做同样的事情 - 是 --dead_strip 吗?

Have you looked into calling gcc with -Os (optimize for size.) I'm not sure if it strips unreached code, but it would be simple enough to test. You could also, after getting your executable back, 'strip' it. I'm sure there's a gcc command-line arg to do the same thing - is it --dead_strip?

阳光下的泡沫是彩色的 2024-09-05 03:54:17

除了用于优化大小的 -Os 之外,链接可能会有所帮助。

In addition to -Os to optimize for size, this link may be of help.

叫嚣ゝ 2024-09-05 03:54:17

自从我问这个问题以来,GCC 4.5 发布了,其中包含一个组合所有文件的选项,因此看起来它只是 1 个巨大的源文件。使用该选项,可以轻松删除未使用的功能。

更多详细信息此处

Since I asked this question, GCC 4.5 was released which includes an option to combine all files so it looks like it is just 1 gigantic source file. Using that option, it is possible to easily strip out the unused functions.

More details here

渡你暖光 2024-09-05 03:54:17

IIRC 默认情况下,链接器会在某些特定情况下执行您想要的操作。缺点是库文件包含一堆目标文件,并且只链接引用的文件。如果你能弄清楚如何让 GCC 将每个函数发送到它自己的目标文件中,然后将其构建到一个库中,你应该得到你在看什么。

我只知道一个编译器实际上可以做到这一点:这里(查看 -lib 标志)

IIRC the linker by default does what you want ins some specific cases. The short of it is that library files contain a bunch of object files and only referenced files are linked in. If you can figure out how to get GCC to emit each function into it's own object file and then build this into a library you should get what you are looking.

I only know of one compiler that can actually do this: here (look at the -lib flag)

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