我正在使用arm微处理器进行编程,并尝试通过UART使用print语句进行调试.我不想stdlibs
只为调试添加.有没有办法打印到控制台没有stdio.h
/ iostream.h
?我可以写自己的printf()
吗?
或者,我可以使用DMA控制器直接写入UART.但是,我想避免这是可能的.使用内置测试功能"echo"或"remote loop-back"我知道我已正确配置UART.
我试图通过重写operator <<来打印出结构中的字段.如果我将覆盖放在一个cpp文件中,这可以正常工作,但是我希望将它放在我的头文件中.
但是,当我这样做时,我得到错误:
multiple definition of `operator<<(std::basic_ostream<char, std::char_traits<char> >&, test)'
Run Code Online (Sandbox Code Playgroud)
是否可以在头文件中?
test.h
#ifndef TEST_H
#define TEST_H
struct test{
int a;
int b;
int c;
};
std::ostream& operator<< (std::ostream& o, const test& t){
o <<"{ " << t.a << " }" << endl;
return o;
}
#endif
Run Code Online (Sandbox Code Playgroud)