小编Lin*_*oln的帖子

在 C 中使用 printf() 重新实现 printf()

我正在制作一个嵌入式Linux项目,我想做一个简单的调试消息库,当我的代码处于生产阶段时,我可以在其中禁用调试消息(使用预编译指令)并替换数据库中某种类型的日志(将来)。我在重新实现时遇到了麻烦,printf()因为va_list. 到目前为止,这是我的代码:

\n

我的源文件:

\n
#include "debug_msgs.h"\n\n#define ENABLE_DEBUG_MSGS   1U\n\n#if ENABLE_DEBUG_MSGS\n\nint print(const char *fmt, ...) {\n    int n = -1;\n    va_list ap;\n    va_start(ap, fmt);\n    n = printf(fmt, ap);\n    va_end(ap);\n    return n;\n}\n\n#else\n\nint print(const char *fmt, ...) {\n    return 0;\n}\n\n#endif\n
Run Code Online (Sandbox Code Playgroud)\n

我的头文件:

\n
#ifndef DEBUG_MSGS_H\n#define DEBUG_MSGS_H\n\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <stdarg.h>\n#include <stdint.h>\n#include <unistd.h>\n\nint print(const char *fmt, ...);\n\n#endif\n
Run Code Online (Sandbox Code Playgroud)\n

我的问题是:\n我的实现可以编译(我知道这并不意味着什么),但是当我做一个简单的测试时,例如:

\n
int num1 = 10;\nint num2 = 100;\nint num3 = 1000;\nfloat floating = 3.14;\nchar str[] = {"Please work, please"};\nint number=-1;\nnumber = …
Run Code Online (Sandbox Code Playgroud)

c linux gcc variadic-functions embedded-linux

5
推荐指数
1
解决办法
140
查看次数

标签 统计

c ×1

embedded-linux ×1

gcc ×1

linux ×1

variadic-functions ×1