Skip to main content
LMore.5
Associate II
September 9, 2021
Question

Newlib Nano link failure for _fstat and _isatty: Other functions link fine.

  • September 9, 2021
  • 2 replies
  • 3083 views

I'm trying to migrate a project from HAL back to SPL as it's less of a haul than porting our code I have to integrate to HAL. The HAL program compiled but at some point during my porting process I'm now getting link errors ONLY for _fstat and _isatty, not any of the other functions defined in the assert.c

The assert.c is the same between a working SPL based project we have and the new one I'm creating, so nothing has changed there. If I implement _fstat in my main.c it still complains that it can't link.

Linker Error:

c:/program files (x86)/gnu arm embedded toolchain/10 2020-q4-major/bin/../lib/gcc/arm-none-eabi/10.2.1/../../../../arm-none-eabi/bin/ld.exe: c:/program files (x86)/gnu arm embedded toolchain/10 2020-q4-major/bin/../lib/gcc/arm-none-eabi/10.2.1/../../../../arm-none-eabi/lib/thumb/v7e-m+dp/hard\libg_nano.a(lib_a-fstatr.o): in function `_fstat_r':

fstatr.c:(.text._fstat_r+0xe): undefined reference to `_fstat'

I've also tried changing and linking against the soft-fp version of the library with no change.

From main.c:

#include <sys/stat.h>

int _fstat (int fd, struct stat* st)

{

 memset (st, 0, sizeof(*st));

 return _swistat (fd, st);

}

    2 replies

    TDK
    Super User
    September 9, 2021

    These are system calls. They should be in "syscalls.c" in your project somewhere, not in the newlib library. If you generate code with CubeMX, it will generate them.

    "If you feel a post has answered your question, please click ""Accept as Solution""."
    LMore.5
    LMore.5Author
    Associate II
    September 9, 2021

    Yes, they are in "syscalls.c" but the newlib nano links against their implementations. For some reason the linker isn't finding the definitions in the syscalls.c or even if I override them in my main.c (as they are declared as __attribute__(weak) to allow overriding in other code)

    Explorer II
    February 23, 2024
    Visitor II
    January 16, 2026

    I encountered the same problem when LTO (link-time optimization) is enabled, with libg_nano.a failing to link against functions defined in syscall.c. Turns out the C library is compiled without LTO, so syscall.c must be compiled with LTO disabled.

    My solution was to append -fno-lto to the compiler flags for syscall.c. This overrides any flags that enable LTO.