c++ - 重新定义 printf()、sprintf() 等 arm-none-eabi 工具链

标签 c++ c arm embedded platformio

我正在使用 gcc-arm-none-eabi 工具链(当前为 7.2.1)设置环境。这适用于 ARM cortex M4 嵌入式设备。

我想为整个项目重新定义 printf,但我遇到了麻烦。我想用 this执行。我已将它安装到项目中,我可以通过调用来使用它,例如: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”(比所有其他包含的 header 更好)包含到要使用 printf 的文件中。

printf_helper.h:

#ifndef PRINTF_HELPER_H
#define PRINTF_HELPER_H

namespace helper {
  typedef int (*printf_t) (const char * format, ...);
  extern const printf_t printf_ptr;
}

#endif /* PRINTF_HELPER_H */

printf_helper.cpp:

#include "printf_helper.h"
#include <cstdio>

namespace helper {
  const printf_t printf_ptr = std::printf;
}

main.cpp 中的使用示例:

// all other included headers
#include "printf_helper.h"

int main() {
  helper::printf_ptr("Hello, %s!\n", "World");
  return 0;
}

关于c++ - 重新定义 printf()、sprintf() 等 arm-none-eabi 工具链,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59366491/

相关文章:

c++ - Cuda blockDim.y 总是==1

c - 指向数组边界问题的指针

c - C语言怎么只写小数位?

arm - eabihf hf代表什么?

c++ - 更改自动分配内存的范围是否会影响性能?

c++ - 使用编译时已知的常量 array1 初始化 C++ array2

c++ - OpenCV 2.4 : undefined reference to cvRand

c - 如何修复段错误

gcc - 如何在 ARM aarch64 GCC 内联汇编中使用 32 位 w 寄存器?

linker - 当 VMA != LMA 时加载 ELF