且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

放置在静态库中时找不到 _sbrk 函数

更新时间:2023-11-17 12:57:37

ld 链接到一个库时,它只会选择那些当时需要的函数(因为引用了在 before 中链接的翻译单元中的函数).链接器会忘记所有其他函数(以后不会考虑库).

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).

因此链接顺序很重要.通常你会链接你的应用程序对象文件(它引用 malloc),然后是标准库(它提供 malloc 并依次引用 _sbrk),然后是提供 _sbrk 的(应用程序)库.

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.

所以链接应该看起来像

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

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

_sbrk 函数由 libapp 提供.

所以要链接的对象的顺序很重要.

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

更新

如其中一条评论所述:如果您在编译期间使用 -g 添加调试符号,那么您还必须链接 libg (-lg).

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

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