c - 减少 C 程序的大小以适应 qr 代码

标签 c gcc optimization compiler-errors linker

我想将扫雷艇装入二维码,其最大容量为 3KB。就目前而言,我的程序的二进制文件为 12KB,其目标文件为 3KB。我尝试在二进制文件上使用 UPX 并设法将其减小到 6KB。我一直在编译这些命令:gcc.exe -Wall -Os -ffunction-sections -fdata-sections -c C:\Users\mlfre\OneDrive\Desktop\Minesweeper2\main.c -o obj\Release\main.ogcc.exe -o bin\Release\Minesweeper2.exe obj\Release\main.o -s -Wl,--gc-sections -lKernel32 -lgcc -lmsvcrt .
我没有尝试删除默认库,因为每当我这样做时,我都会遇到很多 undefined reference 错误,并且尝试了很多方法,但每个“解决方案”都会带来更多问题。我还没有尝试编辑 PE header ,因为它们看起来非常复杂,而且我还没有找到关于如何编辑的足够低级别的解释。尽管我确实确保限制了我以前使用的头文件 windows.h .我知道我需要的库是 kernel32因为大多数人都需要那个,mscvrt因为它的 windows.h 库,和 gcc因为如果我使用 -nodefaultlibs 它将被删除。
我的代码可以在这里看到:

#include <windows.h>

#define WIDTH 100
#define HEIGHT 100
#define BOMBS 800

struct xorshift_state {
  int a;
};

int xorshift(struct xorshift_state *state)
{
    int x = state->a;
    x ^= x << 13;
    x ^= x >> 17;
    x ^= x << 5;
    return state->a = x;
}

void ExpandGrid(int fullGrid[WIDTH][HEIGHT], int knownGrid[WIDTH][HEIGHT], int blankPos[2])
{
    int neighbors[8][2] = {{0,1}, {1,0}, {1,1},
                          {0,-1},        {-1,0},
                          {-1,-1},{-1,1},{1,-1}};
    int curTile[2];

    knownGrid[blankPos[0]][blankPos[1]] = 1;
    if(fullGrid[blankPos[0]][blankPos[1]] != 0) return;

    for(int blck = 0; blck < 8; ++blck)
    {
        curTile[0] = blankPos[0]+neighbors[blck][0];
        curTile[1] = blankPos[1]+neighbors[blck][1];
        if(curTile[0] > WIDTH-1 || curTile[1] > HEIGHT-1 || curTile[0] < 0 || curTile[1] < 0) continue;

        if(fullGrid[curTile[0]][curTile[1]] == 0 && knownGrid[curTile[0]][curTile[1]] == 0)
        {
            knownGrid[curTile[0]][curTile[1]] = 1;
            ExpandGrid(fullGrid, knownGrid, curTile);
        }
        else if(fullGrid[curTile[0]][curTile[1]] > 0) knownGrid[curTile[0]][curTile[1]] = 1;
    }
}

int main(int argc, char *argv[])
{

    COORD characterBufferSize = { WIDTH, HEIGHT };
    COORD characterPosition = { 0, 0 };
    SMALL_RECT consoleWriteArea = { 0, 0, WIDTH - 1, HEIGHT - 1 };
    CHAR_INFO consoleBuffer[WIDTH][HEIGHT];

    HANDLE wHnd = GetStdHandle(-11);
    HANDLE rHnd = GetStdHandle(-10);

    DWORD numEventsRead = 0;
    DWORD numEvents = 0;
    INPUT_RECORD *eventBuffer = {0};
    int wait = 45;

    int startGrid[WIDTH][HEIGHT] = { 0 };
    int knownGrid[WIDTH][HEIGHT] = { 0 };
    int arrowPos[2] = {0, 0};
    int bomb[2] = {0};
    struct xorshift_state seed = {argc == 2 ? (int) argv[1] : 1};

    for (int i = 0; i < BOMBS; i++)
    {
        while (startGrid[bomb[0]][bomb[1]] < -1 || bomb[0] <= 0 || bomb[1] <= 0 || bomb[0] >= WIDTH-1 || bomb[1] >= HEIGHT-1)
        {
            bomb[0] = (xorshift(&seed) % WIDTH-1) + 1;
            bomb[1] = (xorshift(&seed) % HEIGHT-1) + 1;
        }

        startGrid[bomb[0]][bomb[1]] = -9;

        startGrid[bomb[0] + 1][bomb[1] + 1]++;
        startGrid[bomb[0] + 1][bomb[1]]++;
        startGrid[bomb[0]][bomb[1] + 1]++;
        startGrid[bomb[0] - 1][bomb[1] + 1]++;
        startGrid[bomb[0]][bomb[1] - 1]++;
        startGrid[bomb[0] + 1][bomb[1] - 1]++;
        startGrid[bomb[0] - 1][bomb[1] - 1]++;
        startGrid[bomb[0] - 1][bomb[1]]++;
    }


    while(1)
    {
        if (arrowPos[0] > WIDTH-1) arrowPos[0] = WIDTH-1;
        if (arrowPos[0] < 0) arrowPos[0] = 0;
        if (arrowPos[1] > HEIGHT-1) arrowPos[1] = HEIGHT-1;
        if (arrowPos[1] < 0) arrowPos[1] = 0;

        for (int x = 0; x < WIDTH; ++x)
        {
            for (int y = 0; y < HEIGHT; ++y)
            {

                if (knownGrid[x][y] == 1)
                {
                    if (startGrid[x][y] > 0)
                    {
                        consoleBuffer[x][y].Char.AsciiChar = '0' + startGrid[x][y];
                        consoleBuffer[x][y].Attributes = 10;
                    }
                    else
                    {
                        consoleBuffer[x][y].Char.AsciiChar = 'o';
                        consoleBuffer[x][y].Attributes = startGrid[x][y] < 0 ? 4 : 17;
                    }
                }
                else
                {
                    consoleBuffer[x][y].Char.AsciiChar = 00;
                    consoleBuffer[x][y].Attributes = 0;
                }

                if(arrowPos[0] == x && arrowPos[1] == y)
                {
                    consoleBuffer[x][y].Attributes = 112;
                }
            }
        }

        WriteConsoleOutput(wHnd, *consoleBuffer, characterBufferSize, characterPosition, &consoleWriteArea);

        numEvents = 0;
        numEventsRead = 0;
        GetNumberOfConsoleInputEvents(rHnd, &numEvents);

        if (numEvents)
        {
            eventBuffer = malloc(sizeof(INPUT_RECORD) * numEvents);
            ReadConsoleInput(rHnd, eventBuffer, numEvents, &numEventsRead);
        }

        if(numEventsRead && wait <= 0)
        {
            wait = 45;
            switch (eventBuffer[0].Event.KeyEvent.wVirtualKeyCode)
            {
                case 38:
                    arrowPos[0]--;
                    break;
                case 40:
                    arrowPos[0]++;
                    break;
                case 37:
                    arrowPos[1]--;
                    break;
                case 39:
                    arrowPos[1]++;
                    break;
                case 13:
                    ExpandGrid(startGrid, knownGrid, arrowPos);
                    break;
            }
        }

        wait--;
    }
}

如果我打开 -nodefaultlibs这是我得到的错误字符串:

-------------- Build: Release in Minesweeper2 (compiler: GNU GCC Compiler)---------------

gcc.exe -Wall -Os -ffunction-sections -fdata-sections  -c C:\Users\mlfre\OneDrive\Desktop\Minesweeper2\main.c -o obj\Release\main.o
gcc.exe  -o bin\Release\Minesweeper2.exe obj\Release\main.o  -s -Wl,--gc-sections -nodefaultlibs  -lKernel32 -lgcc -lmsvcrt
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../crt2.o:(.text+0x13c): undefined reference to `fesetenv'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../crt2.o:(.text+0x1a5): undefined reference to `__dyn_tls_init_callback'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../crt2.o:(.text+0x1d9): undefined reference to `__cpu_features_init'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../crt2.o:(.text+0x1de): undefined reference to `_CRT_fenv'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../crt2.o:(.text+0x1e6): undefined reference to `fesetenv'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../crt2.o:(.text+0x1eb): undefined reference to `_setargv'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../crt2.o:(.text+0x1f0): undefined reference to `_CRT_fmode'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../crt2.o:(.text+0x206): undefined reference to `_pei386_runtime_relocator'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../crt2.o:(.text+0x25d): undefined reference to `_CRT_fmode'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../crt2.o:(.text+0x271): undefined reference to `_CRT_fmode'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../crt2.o:(.text+0x2a4): undefined reference to `_CRT_glob'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
12 error(s), 0 warning(s) (0 minute(s), 0 second(s))
所以现在我想知道 a) 我如何解决这个 nodefaultlibs 错误,或者 b) 还有什么我可以做的,我可能忽略了减少程序的大小?

最佳答案

尝试使用“编程”技巧来改进您的程序。比尔盖茨在他年轻的时候其实是一个非常好的程序员,从他那里读了一些编程例子。
使用较小的数据类型,例如,使用 char 而不是 int(在可能的情况下),或使用 int16 而不是 int32。
去除冗余数据,例如:

#define WIDTH 100
#define HEIGHT 100
仅使用:
#define DIMENSION 100
不要使用 struct .
如果可能,不要使用缓冲区。
不要使用 int argc, char *argv[] .
使用键盘作为唯一的输入设备。
不要使用这些类型的数组:
int neighbors[8][2] = {{0,1}, {1,0}, {1,1},
                      {0,-1},        {-1,0},
                      {-1,-1},{-1,1},{1,-1}};
改用 bool 表: int32 邻居 = 0b 0001 0100 0101 0011 1100 1111 1101 0111 (这个 bool 表只需要 32 位内存,你的数组需要 8 x 2 x 32 位 = 512 位,半 Kbit ;-))
使用最佳最小化设置编译您的程序,也就是 6KB 程序版本。用反汇编器和十六进制编辑器打开你编译的程序,删除所有不需要的东西,比如纯文本和其他开销等。请注意,这是很多工作。

关于c - 减少 C 程序的大小以适应 qr 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63552420/

相关文章:

c - 段故障strtok

c - 分配/"ISO C90 forbids mixed declarations and code"

gcc - 如何选择GCC使用的汇编器?

c - 使用 fscanf 从文件中读取数据流

c - 指向不在C中的列表中的项目

c - 这段代码有什么问题?试图显示复制的字符数组行

c++ - 使用 GCC 优化 C/C++ 循环中的嵌套 if 语句

c++ - vector 、矩阵和四元数的缓存性能

java - 如何通过 Java 使用 Fico Xpress (Mosel)?

c++ - 在条件下更新变量的最快方法是什么?