从 gcc 和 mingw 中删除不需要的代码

发布于 2024-10-17 04:08:41 字数 279 浏览 0 评论 0原文

我注意到 mingw 在调用 main() 之前添加了很多代码,我假设它用于解析命令行参数,因为其中一个函数称为 __getmainargs(),并且还添加了很多字符串到最终的可执行文件中,例如 mingwm.dll和一些错误字符串(如果应用程序崩溃)表示 mingw 运行时错误或类似的内容。

我的问题是:有没有办法删除所有这些东西?我不需要所有这些东西,我尝试了 tcc(微型 C 编译器),它完成了这项工作。但不是像 gcc (solaris/mac) 这样的跨平台

有什么想法吗?

谢谢。

i noticed that mingw adds alot of code before calling main(), i assumed its for parsing command line parameters since one of those functions is called __getmainargs(), and also lots of strings are added to the final executable, such as mingwm.dll and some error strings (incase the app crashed) says mingw runtime error or something like that.

my question is: is there a way to remove all this stuff? i dont need all these things, i tried tcc (tiny c compiler) it did the job. but not cross platform like gcc (solaris/mac)

any ideas?

thanks.

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

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

发布评论

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

评论(2

遇到 2024-10-24 04:08:41

是的,您确实确实需要所有这些东西。它们是您的代码运行所在的 C 环境的启动和拆卸代码。

除了低级嵌入式解决方案等非托管环境之外,您会发现几乎所有 C 环境都有一些东西像那样。例如某些类 UNIX 操作系统下的 /lib/crt0.o 或 Windows 下的 crt0.obj

它们对于代码的成功运行至关重要。您可以随意省略不使用的库函数(printfabs 等),但需要启动代码。


它可能执行的一些操作包括 atexit 结构的初始化、参数解析、C 运行时库结构的初始化、C/C++ pre-main 值的初始化以及等等。

它是高度特定于操作系统的,如果您不想做某些事情,您可能必须获取它的源代码并将其删除,本质上是为目标文件提供您自己的精简替换。

Yes, you really do need all those things. They're the startup and teardown code for the C environment that your code runs in.

Other than non-hosted environments such as low-level embedded solutions, you'll find pretty much all C environments have something like that. Things like /lib/crt0.o under some UNIX-like operating systems or crt0.obj under Windows.

They are vital to successful running of your code. You can freely omit library functions that you don't use (printf, abs and so on) but the startup code is needed.


Some of the things that it may perform are initialisation of atexit structures, argument parsing, initialisation of structures for the C runtime library, initialisation of C/C++ pre-main values and so forth.

It's highly OS-specific and, if there are things you don't want to do, you'll probably have to get the source code for it and take them out, in essence providing your own cut-down replacement for the object file.

天涯离梦残月幽梦 2024-10-24 04:08:41

您可以放心地假设您的工具链不包含不需要需要且可以安全地排除的代码。

确保编译时没有调试信息,并对生成的可执行文件运行 strip。任何比这更具侵入性的事情都需要对您的工具链有深入的了解,并且可能会导致难以调试的相当奇怪的行为 - 即,如果您必须询问如何完成它,您不应该尝试这样做。

You can safely assume that your toolchain does not include code that is not needed and could safely be left out.

Make sure you compiled without debug information, and run strip on the resulting executable. Anything more intrusive than that requires intimate knowledge of your toolchain, and can result in rather strange behaviour that will be hard to debug - i.e., if you have to ask how it could be done, you shouldn't try to do it.

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