_sbrk function not found when placed in a static library

后端 未结 1 617
野的像风
野的像风 2021-01-13 23:01

I\'m creating a bare-metal application for the stm32f407 microcontroller, which has an ARM Cortex M4 core. As such, I\'m delivering the implementation of functions like

相关标签:
1条回答
  • 2021-01-13 23:24

    When ld links against a library, it will only pick those functions which are required at that time (because of references to functions from translation units which have been linked in before). The linker will forget all other functions (and the library won't be considered later).

    Therefore the linking order does matter. Normally you would link in your application object file (which references malloc), then the standard library (which provides malloc and in turn references _sbrk), and then your (application) library which provides _sbrk.

    So linking should look like

    arm-none-eabi-gcc ... -o out.elf startup.o main.o -lc -lm -lapp

    with the _sbrk function being provided by libapp.

    So the order of the objects to be linked does matter.

    Update

    As stated in one of the comments: If you add debug symbols using -g during compilation, then you have to link against libg as well (-lg).

    arm-none-eabi-gcc ... -o out.elf startup.o main.o -lc -g -lm -lapp

    0 讨论(0)
提交回复
热议问题