从 gcc 和 mingw 中删除不需要的代码
我注意到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您确实确实需要所有这些东西。它们是您的代码运行所在的 C 环境的启动和拆卸代码。
除了低级嵌入式解决方案等非托管环境之外,您会发现几乎所有 C 环境都有一些东西像那样。例如某些类 UNIX 操作系统下的
/lib/crt0.o
或 Windows 下的crt0.obj
。它们对于代码的成功运行至关重要。您可以随意省略不使用的库函数(
printf
、abs
等),但需要启动代码。它可能执行的一些操作包括
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 orcrt0.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.
您可以放心地假设您的工具链不包含不需要需要且可以安全地排除的代码。
确保编译时没有调试信息,并对生成的可执行文件运行
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.