c++ - 将 LTO 与 arm-none-eabi 和 newlib-nano 结合使用

标签 c++ gcc arm newlib link-time-optimization

我正在为 STM32F103 开发一个裸机嵌入式项目,我正在使用 GNU ARM Embedded version 7-2017-q4-major工具链。我目前正在通过 GNU ARM Eclipse 进行编译。

我正处于需要开始优化项目以提高速度的地步,作为要做的第一件事,我当然尝试打开所有优化器标志。其他一切都很好,但是当我尝试使用 -flto 打开链接时间优化时,我在最后一步遇到了链接器错误:

Invoking: Cross ARM C++ Linker
arm-none-eabi-g++ -mcpu=cortex-m3 -mthumb -O3 -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -flto -Wall -Wextra  -g3 -T mem.ld -T libs.ld -T sections.ld -nostartfiles -Xlinker --gc-sections -L"../ldscripts" -Wl,-Map,"Project.map" -Xlinker --cref --specs=nano.specs -o "Project.elf"  ./tiny-mt/tinymt/tinymt32.o  ... .o   
/Users/me/opt/gcc-arm-none-eabi-7-2017-q4-major/bin/../lib/gcc/arm-none-eabi/7.2.1/../../../../arm-none-eabi/lib/thumb/v7-m/libg_nano.a(lib_a-fstatr.o): In function `_fstat_r':
fstatr.c:(.text._fstat_r+0xe): undefined reference to `_fstat'
/Users/me/opt/gcc-arm-none-eabi-7-2017-q4-major/bin/../lib/gcc/arm-none-eabi/7.2.1/../../../../arm-none-eabi/lib/thumb/v7-m/libg_nano.a(lib_a-isattyr.o): In function `_isatty_r':
isattyr.c:(.text._isatty_r+0xc): undefined reference to `_isatty'
collect2: error: ld returned 1 exit status
make: *** [Project.elf] Error 1

这显然是因为 newlib-nano 没有使用 LTO 编译?

那么我该如何让它发挥作用呢?我想我可以试试 compiling newlib-nano myself并添加必要的标志(并更改工具以使用 -gcc-ar 等),但我想/希望有人已经这样做了?我的 google-fu 不足以找到任何有用的东西。

最佳答案

nosys.specs指定链接 -lnosys 应为 _fstat 提供 stub 实现以及 _isatty 和其他标准/posix 函数。
来自 gcc manual link options :

-llibrary
Search the library named library when linking. ...
It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.

因此,如果您将 --specs=nano.specs 移动到链接命令的末尾,您的源代码将与 -lnosys 链接并正确使用 _isatty _fstat 来自 libnosys 库的实现。像这样:

arm-none-eabi-g++ -mcpu=cortex-m3 -mthumb -O3 -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -flto -Wall -Wextra  -g3 -T mem.ld -T libs.ld -T sections.ld -nostartfiles -Xlinker --gc-sections -L"../ldscripts" -Wl,-Map,"Project.map" -Xlinker --cref -o "Project.elf"  ./tiny-mt/tinymt/tinymt32.o  ... .o --specs=nano.specs

我可以猜测没有 LTO 编译的 newlib-nano 与它无关。我将多个项目与 LTO 和 newlib-nano 一起使用,它们工作得非常完美。通常 LTO 工作得很好,删除了抽象函数层,可预测,优化非常好,但我只有 2 年的使用经验。如果我真的需要速度(并且可以忍受非标准行为),我会使用 -Ofast -flto -fno-fat-lto-objects

关于c++ - 将 LTO 与 arm-none-eabi 和 newlib-nano 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50716121/

相关文章:

c - 从 RAM ARM Cortex M4 执行

C++ 可执行文件有时会无缘无故地快 3 倍

c++ - 我想把IpLmage转mat,opencv的时候遇到一些错误

c++ - Directx : HLSL Texture based height map.(着色器 5.0)

c - strerror_r 无效

将 == op 与有符号 var 和无符号文字一起使用时,GCC 不会发出警告

c++ - gcc code::blocks 共享库问题

c - bpkt ARM 指令卡住了我的嵌入式应用程序

eclipse - Eclipse CDT + GNU ARM 中的奇怪包含路径

c++ - C++中最快的输入法是什么