AnsweredAssumed Answered

Linker error "Undefined reference to 'xxx' "

Question asked by LPCware Support on Mar 31, 2016

The simplest reason for seeing undefined references when linking is that there is a problem with your own code base. For example, you may have misspelt the name of one of the functions that you call, or you may not have linked in a library project. However a very common reason for such errors is that you are linking with the wrong C library variant!

 

If you linking against the Newlib(none) or Redlib(none) C library variants and are using functions from the C library such as printf or malloc you will get errors about undefined references when linking. Fuller examples of these error messages can be seen lower down this FAQ.

 

In such circumstances you will need to change which variant of the C library you are linking:

 

  • Redlib: to resolve the the undefined references, use:
    • Redlib(nohost), or
    • Redlib(semihost)
  • Newlib: to resolve the the undefined references, use:
    • Newlib(nohost), or
    • Newlib(semihost)

 

Details of how to do this can be found in the FAQ "Switching the selected C library".

 

For an explanation of the library variants, please see the FAQ "What are none, nohost and semihost libraries?".

 

Undefined references with Redlib

 

The below is an example set of errors encountered when linking an application that calls printf() using Redlib...

In function `printf':
fpprintf.c:(.text.printf+0x36): undefined reference to `__sys_write'

In function `__flsbuf':
_deferredlazyseek.c:(.text.(float, long, short, bool, float __restrict)+0x92):
undefined reference to `__sys_istty'


In function `_Cwritebuf':
_writebuf.c:(.text._Cwritebuf+0x18): undefined reference to `__sys_flen'
_writebuf.c:(.text._Cwritebuf+0x2e): undefined reference to `__sys_seek'
_writebuf.c:(.text._Cwritebuf+0x56): undefined reference to `__sys_write'

In function `malloc':
alloc.c:(.text.malloc+0x70): undefined reference to `_sbrk'
alloc.c:(.text.malloc+0x92): undefined reference to `_sbrk'

In function `_Csys_alloc':
alloc.c:(.text._Csys_alloc+0xe): undefined reference to `__sys_write'
alloc.c:(.text._Csys_alloc+0x12): undefined reference to `__sys_appexit'

In function `fseek':
fseek.c:(.text.fseek+0x1c): undefined reference to `__sys_istty'
fseek.c:(.text.fseek+0x3e): undefined reference to `__sys_flen'

Undefined references with Newlib

 

The below is an example set of errors encountered when linking an application that calls printf() using Newlib...

In function `_sbrk_r':
sbrkr.c:(.text+0xc): undefined reference to `_sbrk'
In function `_write_r':
writer.c:(.text+0x10): undefined reference to `_write'
In function `_close_r':
closer.c:(.text+0xc): undefined reference to `_close'
In function `_fstat_r':
fstatr.c:(.text+0xe): undefined reference to `_fstat'
In function `_isatty_r':
isattyr.c:(.text+0xc): undefined reference to `_isatty'
In function `_lseek_r':
lseekr.c:(.text+0x10): undefined reference to `_lseek'
In function `_read_r':
readr.c:(.text+0x10): undefined reference to `_read'
In function `abort':
abort.c:(.text+0xa): undefined reference to `_exit'
In function `_kill_r':
signalr.c:(.text+0xe): undefined reference to `_kill'
In function `_getpid_r':
signalr.c:(.text+0x28): undefined reference to `_getpid'

Outcomes