【问题标题】:MinGW, how to avoid linking statically full libstdc++MinGW,如何避免链接静态完整的 libstdc++
【发布时间】:2017-07-29 08:48:43
【问题描述】:

我在 cygwin 中使用 mingw 64 位。

我知道如果我编译使用

x86_64-w64-mingw32-g++.exe -std=c++11 hello.cpp

除非在 Path 环境变量中指定 libstdc++ 和其他库的库路径,否则输出 .exe 不会运行。

另一种方法是静态链接

x86_64-w64-mingw32-g++.exe -std=c++11 hello.cpp -static-libgcc -Wl,-Bstatic -lstdc++ -lpthread

因为我想要一个可以轻松复制到不同机器上的单个 .exe,所以第二种解决方案对我来说更好。我唯一的问题是,由于我是静态链接的,即使对于一个简单的 helloworld 程序,可执行文件的大小也会超​​过 10 Mb。所以我的问题是:是否可以仅静态链接程序实际使用的库部分?

【问题讨论】:

    标签: c++ cygwin static-linking mingw-w64


    【解决方案1】:

    Windows 上的 binutils 链接器 ld 不正确支持 --gc-sections 参数,该参数与编译器标志 -ffunction-sections 和 -fdata-sections 结合使用,允许链接器丢弃未使用的代码块。

    你真不走运。您唯一能做的就是通常的做法:剥离可执行文件(通过在链接后对其运行 strip 命令)并使用 -Os 编译优化大小的代码。


    请记住,这些选项在 Windows 上不起作用(就本答案而言,包括 Cygwin 平台),您通常可以这样做:

    g++ -c -Os -ffunction-sections -fdata-sections some_file.cpp -o some_file.o
    g++ -c -Os -ffunction-sections -fdata-sections main.cpp -o main.o
    g++ -Wl,--gc-sections main.o some_file.p -o my_executable
    

    【讨论】:

    • 我以前从未执行过这些操作。您介意提供更多细节或示例吗? ——
    猜你喜欢
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2017-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多