历史上的今天
今天是:2025年04月08日(星期二)
2019年04月08日 | STM32 printf 重定向问题
2019-04-08 来源:eefocus
1、declaration conflicts with target of using declaration already in scope
不能使用.cpp文件,若要使用printf重定向,那就使用.c 文件
解决方法:
1、在main文件中包含 “stdio.h” 文件
2、在工程中创建一个文件保存为 Regtarge.c , 然后将其添加工程中
在文件中输入如下内容(直接复制即可)
#include
#include
#pragma import(__use_no_semihosting_swi)
extern int SendChar(int ch); // 声明外部函数,在main文件中定义
extern int GetKey(void);
struct __FILE {
int handle; // Add whatever you need here
};
FILE __stdout;
FILE __stdin;
int fputc(int ch, FILE *f) {
return (SendChar(ch));
}
int fgetc(FILE *f) {
return (SendChar(GetKey()));
}
void _ttywrch(int ch) {
SendChar (ch);
}
int ferror(FILE *f) { // Your implementation of ferror
return EOF;
}
void _sys_exit(int return_code) {
label: goto label; // endless loop
}
3、在main文件中添加定义以下两个函数
int SendChar (int ch) {
while (!(USART1->SR & USART_FLAG_TXE)); // USART1 可换成你程序中通信的串口
USART1->DR = (ch & 0x1FF);
return (ch);
}
int GetKey (void) {
while (!(USART1->SR & USART_FLAG_RXNE));
return ((int)(USART1->DR & 0x1FF));
}
另外一种配置方法(非解决方法)
1、首先要在你的main 文件中 包含“stdio.h” (标准输入输出头文件)。
2、在main文件中重定义
// 发送数据
int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (unsigned char) ch);// USART1 可以换成 USART2 等
while (!(USART1->SR & USART_FLAG_TXE));
return (ch);
}
// 接收数据
int GetKey (void) {
while (!(USART1->SR & USART_FLAG_RXNE));
return ((int)(USART1->DR & 0x1FF));
}
这样在使用printf时就会调用自定义的fputc函数,来发送字符。
3、在工程属性的 “Target" -> "Code Generation" 选项中勾选 "Use MicroLIB"”
MicroLIB 是缺省C的备份库,关于它可以到网上查找详细资料。
下一篇:STM32F4实现矩阵键盘
史海拾趣
|
本帖最后由 jameswangsynnex 于 2015-3-3 19:57 编辑 (文中S—发射端,R—接收端) 今日有一让大家的使用手机而无需缴纳话费的设想,愿与大家共同分享,也希望其中的一些尚未解决之处大家集思广益。 首先,为什么我们要向移动、联通等运营商 ...… 查看全部问答> |
|
网上有文章说,使用从CWnd派生类中的GetDC()和ReleaseDC()会出现内存泄漏,不知道是否属实? 我使用以下代码进行测试: 使用一Timer,进行窗口Invalidate()。 void CTestDCDlg::OnPaint() { //CPaintDC dc(this); // device con ...… 查看全部问答> |
|
数据库同步 SqlCeRemoteDataAccess.Pull 里面的trackoption 参数设置问题 需要同步PDA和服务器数据库。用SQLCE3.0 SqlCeRemoteDataAccess.Pull方法可以下载服务器数据库的数据到PDA, 其中有个参数叫trackoption 。 当我把这个参数设置为TRACKINGOFF,也就是不跟踪数据变化时,没有任何问题,可以下载数据。 但当把它 ...… 查看全部问答> |
|
一块段显小屏,LCM046 一个单片机STC89C58RD+,一个串口下载器。 要完成一个大任务的一个小部分-----------点亮段式LCD。 呵呵,这个对我来说小菜一盘,没太在意。 因为我以前就点亮过只不过那个东东存在(对人 ...… 查看全部问答> |




