c 二进制文件大于源文件

c binary file greater than source file

我有一个名为 simple.c 的 c 源文件(文件大小 68 字节 ),我使用 gcc 编译了它。输出二进制文件大小为 7151 字节.

C源代码:

int main()
{
    int a = 10;
    int b = 34;
    int c = a + b;
    return c;
}

我没有包含任何头文件。

我不知道,C二进制文件怎么变得比源文件大了。谁能解释一下?

test@test-desktop:~/Desktop/c$ ls -l
total 4
-rw-rw-r-- 1 test test 68 Jul 15 15:04 simple.c
test@test-desktop:~/Desktop/c$ gcc simple.c 
test@test-desktop:~/Desktop/c$ ls -l
total 12
-rwxrwxr-x 1 test test 7151 Jul 15 15:04 a.out
-rw-rw-r-- 1 test test   68 Jul 15 15:04 simple.c

以上是终端输出

这将是调试和符号信息。优化并剥离它:

  1. 使用 gcc -Os 优化最小尺寸。
  2. 使用strip a.out删除符号和调试信息。

还有 CRT 启动文件提供了处理 argc、argv 等和设置环境的大量二进制文件。您可以使用 -nostartfiles 选择退出那些,但您可能不想这样做。

即使不包含任何内容,您也有一定的一次性开销。在 main() 之前有代码 运行(正在设置 stdinstdoutstderrsignal handler tables etc.), and code running after that function returns (e.g. checking for anything registered with atexit() and similar things). This is called the C runtime, traditionally located in crt0.o,它链接到任何可执行文件.