简体   繁体   English

对 _sbrk 的未定义引用

[英]Undefined reference to _sbrk

I am having a problem with _sbrk .我遇到了_sbrk问题。 In a link phase of compilation i use below comand to link my objects and i get undefined reference to _sbrk .在编译的链接阶段,我使用下面的命令来链接我的对象,并且我得到了对_sbrk未定义引用。

arm-none-eabi-ld -static -T linkerscript.ld -o exe timer_example.o /home/ziga/projects/cs_lite/arm-none-eabi/lib/libc.a /home/ziga/projects/cs_lite/lib/gcc/arm-none-eabi/4.5.1/libgcc.a

I am compiling for arm926ej-s and in ARM mode so i think i have chosen the right multilib ( libc.a and libgcc.a ) which is located in folder home/ziga/projects/cs_lite/arm-none-eabi/lib/ .我正在编译arm926ej-s和 ARM 模式,所以我想我选择了正确的 multilib libc.alibgcc.a ,它位于文件夹home/ziga/projects/cs_lite/arm-none-eabi/lib/ .

I have been searching internet for _sbrk function and it is some sort of a memory managment call which isnt included in standard C libraries as it is dependant on microprocessor.我一直在互联网上搜索_sbrk函数,它是某种内存管理调用,它不包含在标准 C 库中,因为它依赖于微处理器。 So do I have to write _sbrk function on my own?那么我必须自己编写_sbrk函数吗? How do I do it?我该怎么做? Do you have any example for arm926ej-s ?你有arm926ej-s 的例子吗? After writing this function I intend to compile it into an object file and link it together with other objects, libraries.写完这个函数后,我打算将它编译成一个对象文件,并将它与其他对象、库链接在一起。

This helps:这有助于:

-mcpu=cortex-m4 -mthumb -specs=nano.specs -specs=nosys.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mcpu=cortex-m4 -mthumb -specs=nano.specs -specs=nosys.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard

The important switches "seem" to be:重要的开关“似乎”是:

-specs=nano.specs -specs=nosys.specs -specs=nano.specs -specs=nosys.specs

I was having the same problem, and adding those to the linker flags helped:我遇到了同样的问题,将这些添加到链接器标志有帮助:

-specs=nano.specs -specs=nosys.specs

Also, just with the nosys.specs fixed the issue, but the code size was a lot bigger.此外,仅使用 nosys.specs 修复了该问题,但代码大小要大得多。

The problem has little to do with _sbrk itself, but rather your attempt to invoke the linker directly, bypassing the compiler driver.问题与_sbrk本身_sbrk ,而是您尝试直接调用链接器,绕过编译器驱动程序。 Instead, use the gcc command to invoke the linker and the -Wl,-linkeroptionhere syntax to pass extra options to the linker.相反,使用 gcc 命令调用链接器和-Wl,-linkeroptionhere语法将额外选项传递给链接器。

One possible solution, if you must invoke the linker yourself.. Try repeating both libc.a and libgcc.a a second time at the end of the command line.一种可能的解决方案,如果您必须自己调用链接器。尝试在命令行末尾libc.a重复libc.alibgcc.a There's also some "as group" linker option you could use to achieve this but I don't know it right off.还有一些“作为组”链接器选项可以用来实现这一点,但我不知道它马上。

recently I also ran into this(again).最近我也遇到了这个(再次)。 the easiest solution which worked for me was to provide/redirect "malloc" and "free" apis to the one available from the SDK on which I was building my application.对我有用的最简单的解决方案是提供/重定向“malloc”和“免费”apis到我构建应用程序的SDK中可用的api。

Basically it happens because of mem management apis missing while linking.基本上这是因为链接时缺少内存管理 API。 like the above answer mentions its not that _sbrk is specifically missing here.就像上面的答案提到的不是 _sbrk 在这里特别丢失。 brk/sbrk syscall intenrally is used for heap management. brk/sbrk 系统调用内部用于堆管理。 hence the _sbrk ,missing link when it comes to mem management apis.因此,当涉及到内存管理 API 时,_sbrk 缺少链接。

I noticed that adding -lnosys (ie libnosys.a) also helped a this to a degree in some integrations.我注意到添加 -lnosys(即 libnosys.a)也在一定程度上帮助了某些集成。

使用visualgdb(使用gcc)和nanolib,我不得不添加链接器标志

-specs=nosys.specs

I solved this problem and will post solution here so i give something back to comunity.我解决了这个问题,并将在此处发布解决方案,以便我回馈社区。 The function _sbrk is located inside NXP CDL package for ARM. _sbrk函数位于 ARM 的 NXP CDL 包内。 Package is available for download (link is for all who dont know this already) here in subfolder CDL_v005/csps/lpc313x/bsps/ea3131/source you will find source file named libnosys_gnu.c which should be added to the project and compiled to object file and after that linked to executable file alongside other objects and libraries.包可供下载(链接是所有谁不知道这已经)在这里的子文件夹CDL_v005/csps/lpc313x/bsps/ea3131/source ,你会发现源文件名为libnosys_gnu.c这应该被添加到该项目,并编译成目标文件,然后链接到可执行文件以及其他对象和库。

Best wishes and a lot of success.最良好的祝愿和很多成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM