Tag: newlib

snprintf()使用newlib nano打印垃圾浮动

我正在运行带有ARM Cortex-M3(STM32F205)的裸机嵌入式系统。 当我尝试使用带浮点数的snprintf()时,例如: float f; f = 1.23; snprintf(s, 20, “%5.2f”, f); 我把垃圾变成了垃圾。 格式似乎很荣幸,即垃圾是一个格式良好的字符串,包含数字,小数点和两个尾随数字。 但是,如果我重复snprintf ,字符串可能会在两次调用之间发生变化。 浮点数学似乎在其他方面起作用,而snprintf与整数一起工作,例如: snprintf(s, 20, “%10d”, 1234567); 我将newlib-nano实现与-u _printf_float链接器开关一起使用。 编译器是arm-none-eabi-gcc 。 我确实怀疑内存分配问题,因为整数打印没有任何打嗝,但浮动表现就好像它们在过程中被破坏一样。 printf系列函数使用浮点数调用malloc ,而不是整数。 我在这个上下文中使用的唯一不属于newlib的代码是我的_sbrk() ,它是malloc所必需的。 caddr_t _sbrk(int incr) { extern char _Heap_Begin; // Defined by the linker. extern char _Heap_Limit; // Defined by the linker. static char* current_heap_end; char* current_block_address; […]

如何获得调用堆栈回溯? (深入嵌入,没有库支持)

我希望我的exception处理程序和调试函数能够打印调用堆栈回溯,基本上就像glibc中的backtrace()库函数一样。 不幸的是,我的C库(Newlib)没有提供这样的调用。 我有这样的事情: #include // GCC’s internal unwinder, part of libgcc _Unwind_Reason_Code trace_fcn(_Unwind_Context *ctx, void *d) { int *depth = (int*)d; printf(“\t#%d: program counter at %08x\n”, *depth, _Unwind_GetIP(ctx)); (*depth)++; return _URC_NO_REASON; } void print_backtrace_here() { int depth = 0; _Unwind_Backtrace(&trace_fcn, &depth); } 这基本上有效,但结果痕迹并不总是完整的。 例如,如果我这样做 int func3() { print_backtrace_here(); return 0; } int func2() { return […]

未定义引用“仅一些math.h”函数

我有一个奇怪的问题。 数学库已添加到我的makefile中。 # include standard C library LDFLAGS += -lc # include standard math library LDFLAGS += -lm 在输出文件(.map)中,我可以看到所有内容都已正确链接: LOAD c:/gnu/powerpc-eabi/3pp.ronetix.powerpc-eabi/bin/../lib/gcc/powerpc-eabi/4.3.3/nof\libgcc.a LOAD c:/gnu/powerpc-eabi/3pp.ronetix.powerpc-eabi/bin/../lib/gcc/powerpc-eabi/4.3.3/../../../../powerpc-eabi/lib/nof\libc.a LOAD c:/gnu/powerpc-eabi/3pp.ronetix.powerpc-eabi/bin/../lib/gcc/powerpc-eabi/4.3.3/../../../../powerpc-eabi/lib/nof\libm.a 当我做 z = pow((double) 2, (double) 3); 它工作正常。 但如果我测试另一个函数,如: double result = asin(x); 我去拿: undefined reference to `asin’ collect2: ld returned 1 exit status 怎么会这样? math.h中提供了pow和asin ,见下文: /* Non reentrant […]