简体   繁体   English

gcc:剥离未使用的函数

[英]gcc: Strip unused functions

I noticed that sometimes even if I don't use iostream and related I/O libraries, my binaries produced by Mingw were still unreasonably large. 我注意到有时即使我不使用iostream和相关的I / O库,我的Mingw生成的二进制文件仍然不合理。

For example, I wrote a code to use vector and cstdio only and compiled it with -O2 -flto , my program can go as large as 2MB! 例如,我编写了一个代码,仅使用vectorcstdio并使用-O2 -flto编译它,我的程序可以大到2MB! I run nm main.exe > e.txt and was shocked to see all the iostream related functions in it. 我运行nm main.exe > e.txt ,很震惊地看到它中所有与iostream相关的功能。

After some googling, I learnt to use -ffunction-sections -Wl,-gc-sections , that reduces the program size from 2MB to ~300KB (if with -s , 100+KB). 经过一些谷歌搜索,我学会了使用-ffunction-sections -Wl,-gc-sections ,将程序大小从2MB减少到~300KB(如果使用-s ,100 + KB)。 Excellent! 优秀!

To further test the effect of -ffunction-sections -Wl,-gc-sections , here is another code: 为了进一步测试-ffunction-sections -Wl,-gc-sections ,这里是另一个代码:

#include <cstdio>
#include <vector>
#include <tuple>
#include <algorithm>
#include <chrono>
#include <windows.h>

#undef min

struct Point {
    int x, y;
};

constexpr int length = 5;
constexpr int half_length() {
    return length & 1 ? length : length - 1;
}

template<class F>
int func_template(F&& f) {
#ifdef _MSC_VER
    puts(__FUNCSIG__);
#else
    puts(__PRETTY_FUNCTION__);
#endif
    printf("\n");
    return f();
}

struct fake_func {
    int operator()() const { return 59; };
};

template<class F, class... Args>
int pass_args(F&& f, Args&&... args) {
#ifdef _MSC_VER
    puts(__FUNCSIG__);
#else
    puts(__PRETTY_FUNCTION__);
#endif
    printf("\n");
    return f(std::forward<Args>(args)...);
}

template<class T>
T min(T x) {
    return x;
}

template<class T, class... Args>
T min(T x, Args... args) {
    T y = min(args...);
    return x < y ? x : y;
}

void type_verifier(int x) {
    printf("%dd ", x);
}

void type_verifier(char x) {
    printf("'%c' ", x);
}

void type_verifier(double x) {
    printf("%lff ", x);
}

template<class T>
void type_verifier(T x) {
    printf("unknown ");
}

template<class T, class... Args>
void type_verifier(T x, Args... args) {
    type_verifier(x);
    type_verifier(args...);
}

int bufLen;
char buf[100];

template<class... Args>
inline int send(Args... args) {
    bufLen = sprintf(buf, std::forward<Args>(args)...);
    return bufLen;
}

namespace std {

inline namespace v1 {
    void func() {
        printf("I am v1\n");
    }
}

namespace v2 {
    void func() {
        printf("I am v2\n");
    }
}

}

int main() {
    std::vector<int> v {1, 2, 3, 4, 5};
    for (auto &i : v) printf("%d ", i);
    printf("\n");

    Point p {1, 2};
    printf("%d %d\n", p.x, p.y);

    auto t = std::make_tuple("Hello World", 12);
    printf("%s %d\n", std::get<0>(t), std::get<1>(t));
    int a, b;
    auto f = []() { return std::make_tuple(1, 2); };
    std::tie(a, b) = f();
    printf("%d %d\n", a, b);

    //int test_constexpr[half_length() + 4];

    int ft = func_template([]{ return 42; });
    printf("func_template: %d\n", ft);
    ft = func_template(fake_func {});
    printf("func_template: %d\n", ft);
    ft = pass_args([](int x, int y) { return x + y; }, 152, 58);
    printf("pass_args: %d\n", ft);
    ft = pass_args([](int n, const char *m) {
        for (int i = 0; i < n; i++) printf("%c ", m[i]);
        printf("\n");
        return 0;
    }, 5, "Hello");

    printf("min: %d\n", min(3, 4, 2, 1, 5));
    type_verifier(12, 'A', 0.5, "Hello");
    printf("\n");

/*  send("Hello World");
    send("%d", 1);
    send("%d", "1234");
    sprintf(buf, "%d", "123");*/

    std::func();
    std::v1::func();
    std::v2::func();

    std::rotate(v.begin(), v.begin() + 2, v.end());
    for (auto &i : v) printf("%d ", i);
    printf("\n");

    auto start = std::chrono::steady_clock::now();

    std::vector<int> x {2, 4, 2, 0, 5, 10, 7, 3, 7, 1}; 
    printf("insertion sort: ");
    for (auto &i: x) printf("%d ", i);
    printf("\n");
    // insertion sort
    for (auto i = x.begin(); i != x.end(); ++i) {
        std::rotate(std::upper_bound(x.begin(), i, *i), i, i+1);
        for (auto &j: x) printf("%d ", j);
        printf("\n");
    }

    std::vector<int> heap {7, 5, 3, 4, 2};
    std::make_heap(heap.begin(), heap.end());
    std::pop_heap(heap.begin(), heap.end());
    printf("Pop heap (%d)\n", heap.back());
    heap.pop_back();
    heap.push_back(1);
    std::push_heap(heap.begin(), heap.end());
    std::sort_heap(heap.begin(), heap.end());
    for (auto &i: heap) printf("%d ", i);
    printf("\n");

    auto end = std::chrono::steady_clock::now();
    auto diff = end - start;
    printf("time: %I64d ms\n",
        std::chrono::duration_cast<std::chrono::milliseconds>(diff).count());

    {
        auto u = v;
        std::move_backward(u.begin(), u.begin() + u.size() - 1, u.begin() + u.size());
        for (auto &i : u) printf("%d ", i);
        printf("\n");
    }

    {
        auto u = v;
        std::move(u.begin() + 1, u.begin() + u.size(), u.begin());
        for (auto &i : u) printf("%d ", i);
        printf("\n");
    }

    start = std::chrono::steady_clock::now();
    Sleep(2000);
    end = std::chrono::steady_clock::now();
    diff = end - start;
    printf("time: %I64d ms\n",
        std::chrono::duration_cast<std::chrono::milliseconds>(diff).count());

    std::chrono::steady_clock::time_point before;
    before = std::chrono::steady_clock::now();
    Sleep(2000);
    auto after = std::chrono::steady_clock::now();
    printf("%f seconds\n", std::chrono::duration<double>(after - before).count());

    return 0;
}

To my disappointment, the final program is once again > 2MB. 令我失望的是,最终的节目再次超过2MB。

Interestingly, cl.exe thoughtfully remove all iostream related functions consistently even if I didn't use /O2 or any other flags, just cl.exe main.cpp . 有趣的是, cl.exe若有所思地删除所有iostream相关的功能一致,即使我没有使用/O2或任何其他标志,只是cl.exe main.cpp (For the code above, cl.exe produces 100+KB binary). (对于上面的代码, cl.exe生成100 + KB二进制文件)。

Did I miss any other useful gcc flags for this? 我错过了其他任何有用的gcc标志吗?

Specification: 规格:

  • Mingw-w64 gcc 6.1.0 Mingw-w64 gcc 6.1.0
  • Mingw-w64 gcc 6.2.0 Mingw-w64 gcc 6.2.0
  • Visual Studio 2017 RC Visual Studio 2017 RC
  • All binaries are linked statically 所有二进制文件都是静态链接的

Compare with Linux 与Linux比较

I compared the binaries produced by gcc 4.9.2 (Linux) and gcc 4.9.3 (mingw-w64) for the above code (except windows.h and Sleep were removed). 我比较了gcc 4.9.2(Linux)和gcc 4.9.3(mingw-w64)为上述代码生成的二进制文件( windows.hSleep除外)。

Compile flag 编译标志

g++ -o c++11 c++11.cpp -std=c++11 -static-libgcc -static-libstdc++ -ffunction-sections -Wl,-gc-sections -O2

Linux gcc did successfully strip away iostream and functions without the need for -flto while Mingw-w64 gcc just can't do it properly. Linux gcc成功地剥离了iostream和功能,而不需要-flto而Mingw-w64 gcc却无法正常运行。

Windows only support PE format while Linux supports ELF format, allowing Linux to use Gold linker. Windows仅支持PE格式,而Linux支持ELF格式,允许Linux使用Gold链接器。 Maybe this is the explanation? 也许这是解释?

Update 更新

I eventually filed a bug at https://sourceforge.net/p/mingw-w64/bugs/578/ . 我最终在https://sourceforge.net/p/mingw-w64/bugs/578/上提交了一个错误。 Let's hope it gets some attentions! 我们希望它得到一些关注!

Try stripping debug and symbol info from static libstdc++ via -Wl,--strip-all . 尝试通过-Wl,--strip-all从静态libstdc ++中-Wl,--strip-all调试和符号信息。 This reduced my executable from 9M to 670K on Cygwin (13x) and from 6M to 80K on Ubuntu (80x). 这使我的可执行文件在Cygwin(13x)上从9M减少到670K,在Ubuntu(80x)上从6M减少到80K。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM