我有一个非常小的 C 程序,它可以反转文件。它在 Windows 上编译为大小为 28,672 字节的 exe
文件。
/O1
和 /Os
似乎没有任何效果)? 顺便说一句 - 当用
gcc
编译时,我得到大约 50Kb 的文件,当用 cl
编译时,我得到 28Kb。编辑:这是代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE *fi, *fo;
char *file1, file2[1024];
long i, length;
int ch;
file1 = argv[1];
file2[0] = 0;
strcat(file2, file1);
strcat(file2, ".out");
fo = fopen(file2,"wb");
if( fo == NULL )
{
perror(file2);
exit(EXIT_FAILURE);
}
fi = fopen(file1,"rb");
if( fi == NULL )
{
fclose(fo);
return 0;
}
fseek(fi, 0L, SEEK_END);
length = ftell(fi);
fseek(fi, 0L, SEEK_SET);
i = 0;
while( ( ch = fgetc(fi) ) != EOF ) {
fseek(fo, length - (++i), SEEK_SET);
fputc(ch,fo);
}
fclose(fi);
fclose(fo);
return 0;
}
更新:
/MD
编译生成一个 16Kb 的文件。 tcc
(Tiny C Compiler) 编译产生了一个 2Kb 的文件。 gcc -s -O2
编译生成一个 8Kb 的文件。 最佳答案
尝试使用 tcc: http://bellard.org/tcc/ 编译它。
关于c - 使 C 程序更小,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13476240/