-
设置Use MicroLIB
-
用CubeMX生成的MDK工程文件替换,重新添加所需的.c文件,原因是使用了printf()函数。
-
用以下代码代替Use MicroLIB
#include "printf_redirect.h"
#include <stdio.h>
#if !defined(_MSC_VER)
#if defined(__CC_ARM)
#pragma import(__use_no_semihosting)
#elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6000000)
__asm(".global __use_no_semihosting\n\t");
#endif
#if defined(__ICCARM__) || defined(__CC_ARM)
struct __FILE
{
int handle;
/* Whatever you require here. If the only file you are using is */
/* standard output using printf() for debugging, no file handling */
/* is required. */
};
#endif
FILE __stdout; // required option '_DLIB_FILE_DESCRIPTOR' for IAR compiler
FILE __stdin;
FILE __stderr;
void _ttywrch(int ch)
{
printf_putchar(ch);
}
void _sys_exit(int x)
{
x = x;
}
int fputc(int ch, FILE *f)
{
printf_putchar(ch);
return ch;
}
int remove(const char *filename)
{
return 0;
}
char *_sys_command_string(char *cmd, int len)
{
return 0;
}
int _sys_tmpnam(char *name, int fileno, unsigned maxlength)
{
return 0;
}
#endif