C++ 重新定义printf()、sprintf()等。arm ONE eabi工具链

C++ 重新定义printf()、sprintf()等。arm ONE eabi工具链,c++,c,arm,embedded,platformio,C++,C,Arm,Embedded,Platformio,我正在使用gcc arm none eabi工具链(目前为7.2.1)进行设置和环境。这是针对ARM cortex M4嵌入式设备的 我想为整个项目重新定义printf,但我遇到了麻烦。我想使用实现。我已经将它安装到项目中,我可以通过调用来使用它,例如:printf_Test:%I,5;一切都按预期进行 现在我想将它设置为默认的printf函数。如果我取消注释:define printf printf_,我会得到以下错误: /home/timv/.platformio/packages/tool

我正在使用gcc arm none eabi工具链(目前为7.2.1)进行设置和环境。这是针对ARM cortex M4嵌入式设备的

我想为整个项目重新定义printf,但我遇到了麻烦。我想使用实现。我已经将它安装到项目中,我可以通过调用来使用它,例如:printf_Test:%I,5;一切都按预期进行

现在我想将它设置为默认的printf函数。如果我取消注释:define printf printf_,我会得到以下错误:

/home/timv/.platformio/packages/toolchain-gccarmnoneeabi@1.70201.0/arm-none-eabi/include/c++/7.2.1/cstdio:127:11: error: '::printf' has not been declared
   using ::printf;
稍后:

src/LoggerTask.cpp:62:5: error: 'printf' was not declared in this scope
在该文件中,我发现了这一行:

#undef printf
当我注释该行时,项目将生成,printf将工作。这很好,但我希望我的项目功能不需要修补工具链


我该怎么做呢?还有哪些信息会有帮助?

您可以使用下一个示例创建指向printf的指针。在本例中,您需要创建名为printf_helper.h和printf_helper.cpp的助手文件。并将printf_helper.h更好地包含在要使用printf的文件的所有其他包含头之后

printf_helper.h: printf_helper.cpp:
将新文件添加到编译中:

#include <stdarg.h>
int vprintf_(const char* format, va_list va);
int printf(const char *str, ...) {
     va_list va;
     va_start(va, str);
     int ret = vprintf_(str, va);
     va_end(va);
     return ret;
}

在创建编译输出时编译并链接结果对象。这就是全部。由于链接器的工作方式,链接器应该选择您的符号,而不是newlib提供的符号。或者,你可以使用-Calp= PrimtFrink选项简单地包装符号。< /P>你需要C解决方案还是只需要C++解决方案?为什么要在LoGruttask.CPP中取消打印?f I uncomment:define printf printf_u-该定义位于哪个文件中?重新定义标准C名称是一个不好的主意,它可能是未定义的行为。您不能在源代码中使用myprintf吗?
#include "printf_helper.h"
#include <cstdio>

namespace helper {
  const printf_t printf_ptr = std::printf;
}
// all other included headers
#include "printf_helper.h"

int main() {
  helper::printf_ptr("Hello, %s!\n", "World");
  return 0;
}
#include <stdarg.h>
int vprintf_(const char* format, va_list va);
int printf(const char *str, ...) {
     va_list va;
     va_start(va, str);
     int ret = vprintf_(str, va);
     va_end(va);
     return ret;
}