使用GCC构建最小的独立可执行文件 [英] Building minimal standalone executable with GCC

查看:71
本文介绍了使用GCC构建最小的独立可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很少有程序(用C语言编写)来实现一些算法,这些算法可用来测量计算时间。整个数据直接在代码中作为静态库实现,这些程序没有输入和输出。也没有C库调用(没有printfs等)。

I have few programs (written in C) implementing some algorithms, that I use to measure computation time. Whole data is implemented as static libraries directly in code, there's no input and output from these programs. There's also no C library calls (no printfs etc.).

我想构建完全独立且最小的可执行文件。我不想将我的程序与libgcc(目标CPU具有协处理器,因此不需要仿真浮点算术),C库或任何其他链接。实际上,我想让我的程序尽可能独立。在Linux上,ELF程序仅需与crt0.o链接即可正常运行,对吧?

I want to build fully independent and minimal executable. I don't want to link my program with libgcc (target CPU has coprocessor, so I don't need to emulate floating point arithmetic), C library or any other. Actually I want to make my program as independent as it's possible. On Linux ELF program has to be linked only with crt0.o to run properly, right?

我主要是因为我很好奇而问问题;)

I'm mostly asking because I'm curious ;)

推荐答案

gcc -nostdlib 链接,然后使用 objdump- h strip --remove-section = ... 可以通过消除诸如注释部分和异常之类的愚蠢的东西来真正减小它的体积处理框架信息部分。继续删除节,直到其停止工作为止。

Link with gcc -nostdlib, then use objdump -h and strip --remove-section=... to really make it small by getting rid of silly things like the comment section and the exception handling frame information sections. Keep removing sections until it stops working.

并使用 -Os 当然编译

这篇关于使用GCC构建最小的独立可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆