我正在制作一个嵌入式Linux项目,我想做一个简单的调试消息库,其中我可以在生产阶段使用预编译指令禁用我的调试消息,并替换为某种类型的数据库日志(将来)。由于va_list,我在重新实现printf()时遇到了问题。这是到目前为止我的代码:
我的源文件:
我收到了以下输出:
我的源文件:
#include "debug_msgs.h"
#define ENABLE_DEBUG_MSGS 1U
#if ENABLE_DEBUG_MSGS
int print(const char *fmt, ...) {
int n = -1;
va_list ap;
va_start(ap, fmt);
n = printf(fmt, ap);
va_end(ap);
return n;
}
#else
int print(const char *fmt, ...) {
return 0;
}
#endif
我的头文件:
#ifndef DEBUG_MSGS_H
#define DEBUG_MSGS_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdint.h>
#include <unistd.h>
int print(const char *fmt, ...);
#endif
我的问题是:
我的实现已经编译通过(我知道这并不表示什么),但当我进行一个简单的测试,比如:
int num1 = 10;
int num2 = 100;
int num3 = 1000;
float floating = 3.14;
char str[] = {"Please work, please"};
int number=-1;
number = print("\nTesting %d %d %d %f %s\n", num1, num2, num3, floating, str);
printf("\nnumber = %d\n",number); //Just to check the return
我收到了以下输出:
Testing -1095005212 100 -1095005212 -0.000002 H*�
number = 52
补充信息:
这是我的平台信息:
Linux mdm9607 3.18.44 #1 PREEMPT Tue Sep 13 19:45:33 UTC 2022 armv7l GNU/Linux
我的编译器信息:
arm-oe-linux-gnueabi-gcc
我的 $CFLAGS:
-O2 -fexpensive-optimizations -frename-registers -fomit-frame-pointer -MMD -MP
我的 $LDFLAGS:
-Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed
LDFLAGS += -lumdp -lumdpcommon
LDFLAGS += -lpthread -lxml2 -lrt
LDFLAGS += -lm
debug_print("%d\n",calculate_a_lot());
,即使debug_print
是无操作指令,你仍然需要调用calculate_a_lot
。请参考我的回答:Distributed Computing with PThread Not Working,了解如何使用调试宏以及调试函数。 - Craig Estey