【发布时间】:2021-07-01 05:09:20
【问题描述】:
我尝试使用 LD_PRELOAD 来挂钩 sprintf 函数,所以我将打印到缓冲区的结果:
#define _GNU_SOURCE
#include <stdio.h>
#include<dlfcn.h>
int sprintf (char * src , const char * format , char* argp)
{
int (*original_func)(char*,const char * , char*);
original_func = dlsym(RTLD_NEXT,"sprintf");
int ret = (*original_func)(src ,format,argp);
FILE* output = fopen("log.txt","a");
fprintf(output,"%s \n",src);
fclose(output);
return ret;
}
当我编译这段代码时gcc -Wall -fPIC -shared -o my_lib.so test_ld.c -ldl
我有错误
test_ld.c:5:5: error: conflicting types for ‘sprintf’
int sprintf (char * src , const char * format , char* argp)
^
In file included from test_ld.c:2:0:
/usr/include/stdio.h:364:12: note: previous declaration of ‘sprintf’ was here
extern int sprintf (char *__restrict __s,
我该如何解决这个问题?
【问题讨论】:
-
该错误是因为 sprintf 的参数类型与之前声明的 on(来自 stdib)不同。 sprintf 的类型是
int sprintf(char *restrict s, const char *restrict format, ...)。我不确定restrict关键字是否必要,但sprintf将可变参数作为第三个参数,而不是指针。
标签: c linux printf ld-preload conflicting-libraries