Skip to content

Commit 1bf1373

Browse files
Implement workaround to get printf("string without args") working
GCC is replacing printf("string with no args"); with puts("string with no args"); even with no optimizations enabled! This is apparently not implemented in a usable way for us in Arduino land and results in a) no output or b) an infinitely rebooting application, depending on whether it's my lucky day or not. Current workaround is to redefine puts() for our needs. Another option is noted in the comment along with the function, in case that bites us in the end.
1 parent 1c2ad49 commit 1bf1373

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/LibPrintf.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,26 @@ extern "C" __attribute__((weak)) void putchar_(char character)
2525
{
2626
print_instance->print(character);
2727
}
28+
29+
#if __GNUC__
30+
/// This works around a problem where GCC is replacing
31+
/// printf("string with no args") with puts("string with no args"), which
32+
/// is not actually implemented in a way that is suitable for us, resulting
33+
/// in an infinite reboot loop or simply not printing that output, depending
34+
/// on my luck.
35+
///
36+
/// This is probably NOT the right way to go about this (expecting it to be undefined
37+
/// behavior, but I have fewer tools available in Arduino land, so I am
38+
/// sticking with this for now.
39+
///
40+
/// If this causes problems in the future, the next thing to try is
41+
/// NOT using the Aliasing option (currently defined in LibPrintf.h) and instead
42+
/// then providing definitions in the mpaland style in our LibPrintf.h header:
43+
/// #define printf printf_
44+
/// #define vprintf vprintf_
45+
/// etc.
46+
extern "C" int puts(const char * str)
47+
{
48+
return printf("%s\n", str);
49+
}
50+
#endif //__GNUC__

0 commit comments

Comments
 (0)