C语言 对 _sbrk 的未定义引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5764414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Undefined reference to _sbrk
提问by 71GA
I am having a problem with _sbrk. In a link phase of compilation i use below comand to link my objects and i get undefined reference to _sbrk.
我在使用 _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.a 和 libgcc.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. Soo do i have to write _sbrk function on my own? How do i do it? Do you have any example for arm926ej-s? After writing this function i intend to compile it into an object file and link it together with other objects, libraries.
我一直在互联网上搜索 _sbrk 函数,它是某种内存管理调用,它不包含在标准 C 库中,因为它依赖于微处理器。Soo 我必须自己编写 _sbrk 函数吗?我该怎么做?你有arm926ej-s的例子吗?写完这个函数后,我打算将它编译成一个对象文件,并将它与其他对象、库链接在一起。
With kind regards, Ziga.
亲切的问候,Ziga。
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. Package is available for download (link is for all who dont know this already) here: http://www.lpclinux.com/Downloads/WebHomeIn 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.
我解决了这个问题,并将在此处发布解决方案,以便我回馈社区。_sbrk 函数位于 ARM 的 NXP CDL 包内。软件包可供下载(链接适用于所有不知道的人):http: //www.lpclinux.com/Downloads/WebHome 在子文件夹 CDL_v005/csps/lpc313x/bsps/ea3131/source 中,您将找到名为的源文件libnosys_gnu.c 应该添加到项目中并编译为目标文件,然后链接到可执行文件以及其他对象和库。
Best wishes and a lot of sucess.
最良好的祝愿和很多成功。
回答by nvd
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
回答by Kaká Barreto
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 修复了该问题,但代码大小要大得多。
回答by R.. GitHub STOP HELPING ICE
The problem has little to do with _sbrkitself, but rather your attempt to invoke the linker directly, bypassing the compiler driver. Instead, use the gcc command to invoke the linker and the -Wl,-linkeroptionheresyntax to pass extra options to the linker.
问题_sbrk本身与问题无关,而是您尝试直接调用链接器,绕过编译器驱动程序。相反,使用 gcc 命令调用链接器和-Wl,-linkeroptionhere将额外选项传递给链接器的语法。
One possible solution, if you must invoke the linker yourself.. Try repeating both libc.aand libgcc.aa second time at the end of the command line. There's also some "as group" linker option you could use to achieve this but I don't know it right off.
一种可能的解决方案,如果您必须自己调用链接器.. 尝试在命令行末尾重复两次libc.a和libgcc.a第二次。还有一些“作为组”链接器选项可以用来实现这一点,但我不知道它马上。
回答by Aj700
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. like the above answer mentions its not that _sbrk is specifically missing here. brk/sbrk syscall intenrally is used for heap management. hence the _sbrk ,missing link when it comes to mem management apis.
基本上这是因为链接时缺少内存管理 API。就像上面的答案提到的不是 _sbrk 在这里特别丢失。brk/sbrk 系统调用内部用于堆管理。因此,当涉及到内存管理 API 时,_sbrk 缺少链接。
I noticed that adding -lnosys (i.e libnosys.a) also helped a this to a degree in some integrations.
我注意到添加 -lnosys(即 libnosys.a)也在一定程度上帮助了某些集成。
回答by jluu
with visualgdb (using gcc), and nanolib, I had to add the linker flag
使用visualgdb(使用gcc)和nanolib,我不得不添加链接器标志
-specs=nosys.specs

