gpt4 book ai didi

编译失败, "error: ‘next_ioctl’ 未声明(首次在此函数中使用)”尽管我包含了 dlfcn.h

转载 作者:行者123 更新时间:2023-11-30 14:20:47 25 4
gpt4 key购买 nike

尝试从 here 编译包装库的示例

我必须包含 stdio.hstdlib.h,然后得出该代码:

#define _GNU_SOURCE
#define _USE_GNU

#include <signal.h>
#include <execinfo.h>
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>

static void show_stackframe() {
void *trace[16];
char **messages = (char **)NULL;
int i, trace_size = 0;

trace_size = backtrace(trace, 16);
messages = backtrace_symbols(trace, trace_size);
printf("[bt] Execution path:\n");
for (i=0; i < trace_size; ++i)
printf("[bt] %s\n", messages[i]);
}

int ioctl(int fd, int request, void *data)
{
char *msg;

if (next_ioctl == NULL) {
fprintf(stderr, "ioctl : wrapping ioctl\n");
fflush(stderr);
// next_ioctl = dlsym((void *) -11, /* RTLD_NEXT, */ "ioctl");
next_ioctl = dlsym(RTLD_NEXT, "ioctl");
fprintf(stderr, "next_ioctl = %p\n", next_ioctl);
fflush(stderr);
if ((msg = dlerror()) != NULL) {
fprintf(stderr, "ioctl: dlopen failed : %s\n", msg);
fflush(stderr);
exit(1);
} else
fprintf(stderr, "ioctl: wrapping done\n");
fflush(stderr);
}
if (request == 1) { /* SCSI_IOCTL_SEND_COMMAND ? */
/* call back trace */
fprintf(stderr, "SCSI_IOCTL_SEND_COMMAND ioctl\n");
fflush(stderr);
show_stackframe();
}
return next_ioctl(fd, request, data);
}

和Makefile

#
# Makefile
#

all: libs test_ioctl

libs: libwrap_ioctl.so

libwrap_ioctl.so: wrap_ioctl.c
rm -f libwrap_ioctl.so*
gcc -fPIC -shared -Wl,-soname,libwrap_ioctl.so.1 -ldl -o libwrap_ioctl.so.1.0 wrap_ioctl.c
ln -s libwrap_ioctl.so.1.0 libwrap_ioctl.so.1
ln -s libwrap_ioctl.so.1 libwrap_ioctl.so

clean:
rm -f libwrap_ioctl.so* test_ioctl

尽管包含了 dlfcn.h,但还是陷入了这些错误。

~/my_src/native/glibc_wrapper > make
rm -f libwrap_ioctl.so*
gcc -fPIC -shared -Wl,-soname,libwrap_ioctl.so.1 -ldl -o libwrap_ioctl.so.1.0 wrap_ioctl.c
wrap_ioctl.c: In function ‘ioctl’:
wrap_ioctl.c:26: error: ‘next_ioctl’ undeclared (first use in this function)
wrap_ioctl.c:26: error: (Each undeclared identifier is reported only once
wrap_ioctl.c:26: error: for each function it appears in.)
make: *** [libwrap_ioctl.so] Ошибка 1

最佳答案

dlfcn.h 本身没有定义任何名为 next_smth 的符号。 (在SUS中,dlfcn.h仅定义了几个dl*函数和RTLD_宏:http://pubs.opengroup.org/onlinepubs/7908799/xsh/dlfcn.h.html)

您应该以明确的方式将其定义为指向程序代码中的函数的指针。像这样的东西:(取自https://port70.net/svn/misc/remac/remac.chttps://github.com/itm/forward-sensor/blob/master/preload.c或...任何谷歌搜索“next_ioctl”):

static int (*next_ioctl) (int fd, int request, void *data) = NULL;

或者,如果您想要集体博客阅读 session ,博客文章中还有带有 ioctl 重载的附加行:http://scaryreasoner.wordpress.com/2007/11/17/using-ld_preload-libraries-and-glibc-backtrace-function-for-debugging/ (就在第一个巨大的代码片段之前)

Then, declare a function pointer to hold the value of the “real” ioctl() function from glibc:

static int (*next_ioctl)(int fd, int request, void *data) = NULL;

Then declare your replacement ioctl function:

关于编译失败, "error: ‘next_ioctl’ 未声明(首次在此函数中使用)”尽管我包含了 dlfcn.h,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15153209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com