简体   繁体   English

我可以使 clang 可执行文件多小?

[英]How small can I make a clang executable?

I'm just testing to see how small I can make this C++ code我只是测试看看我能把这个 C++ 代码做多小

#include <iostream>
using namespace std;

int main() {
    cout << "hi";
}

using this zsh command:使用这个 zsh 命令:

g++ test.cpp -Os -g -o main

but the smallest I can make it is 52 Kb, so is there any way to make it even smaller?但是我能做到的最小的是 52 Kb,那么有没有办法让它更小呢?

Using your code and compiler flags with g++ 11.2 I get a 35,8kB executable (different result could be explained by using a different compiler version or target system).将您的代码和编译器标志与g++ 11.2一起使用,我得到一个35,8kB的可执行文件(可以通过使用不同的编译器版本或目标系统来解释不同的结果)。

  1. Removing the -g flag brings the output file's size down to 16,3kB .删除-g标志会使 output 文件的大小降至16,3kB
  2. You can modify your code without changing its functionality in the following way ( 16,0kB ):您可以通过以下方式( 16,0kB )修改代码而不更改其功能:
#include <stdio.h>

int main(){
    fputs("hi", stdout);
    return 0;  // doesn't change the output, only here for completeness sake
}
  1. Using clang to compile above code further reduces output executable's size to 15,9kB .使用clang编译上述代码进一步将 output 可执行文件的大小减少到15,9kB

  2. Adding the -s flag: 14,5kB添加-s标志: 14,5kB

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

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