gpt4 book ai didi

c - 为什么链接到 FastCGI 库会导致段错误?

转载 作者:太空宇宙 更新时间:2023-11-04 08:49:26 24 4
gpt4 key购买 nike

我有一个程序正在使用名为“wjelement”的库,每当我尝试将此库与 FastCGI 一起使用时,我都会遇到段错误。我在下面做了一个简化的测试用例。如果我在没有 fcgi_stdio.h 的情况下编译代码并且不链接库,代码工作正常,如果我添加 fastcgi header 并链接它,我会得到一个段错误,即使我不使用任何快速 cgi 调用。

在我的 FastCGI 代码中,反之亦然,如果我删除 WJelement 代码,程序的其余部分工作正常。

我不确定我是否需要责怪我的程序、FastCGI 库或 WJElement 库...

#include <stdio.h>
#include <fcgi_stdio.h>
#include <wjreader.h>

int main (int argc, char *argv[]) {

FILE *my_schema_file;
my_schema_file = fopen("test_schema.json", "rb");

if (my_schema_file == NULL) {
printf("Failed to open test schema file\n");
return 1;
} else {
printf("Opened test schema file\n");
}

WJReader my_schema_reader;
my_schema_reader = WJROpenFILEDocument(my_schema_file, NULL, 0);

if (my_schema_reader == NULL) {
printf("Failed to open test schema reader\n");
return 1;
} else {
printf("Opened test schema reader\n");
}

return 0;
}

GDB 回溯:

Program received signal SIGSEGV, Segmentation fault.
0x0000003e19e6c85f in __GI__IO_fread (buf=0x6023c4, size=1, count=2731, fp=0x602250) at iofread.c:41
41 _IO_acquire_lock (fp);
(gdb) backtrace
#0 0x0000003e19e6c85f in __GI__IO_fread (buf=0x6023c4, size=1, count=2731, fp=0x602250) at iofread.c:41
#1 0x00007ffff7dde5d9 in WJRFileCallback () from /lib/libwjreader.so.0
#2 0x00007ffff7dde037 in WJRFillBuffer () from /lib/libwjreader.so.0
#3 0x00007ffff7dde4e9 in _WJROpenDocument () from /lib/libwjreader.so.0
#4 0x000000000040081f in main (argc=1, argv=0x7fffffffdeb8) at test.c:20

最佳答案

在这里找到答案:http://www.fastcgi.com/devkit/doc/fcgi-devel-kit.htm

If your application passes FILE * to functions implemented in libraries for which you do not have source code, then you'll need to include the headers for these libraries before you include fcgi_stdio.h

然后我必须使用 FCGI_ToFILE(FCGI_FILE *); 从 FCGI_FILE * 转换为 FILE *;

#include <stdio.h>
#include <wjreader.h>
#include <fcgi_stdio.h>

int main (int argc, char *argv[]) {

FILE *my_schema_file;
my_schema_file = fopen("test_schema.json", "rb");

if (my_schema_file == NULL) {
printf("Failed to open test schema file\n");
return 1;
} else {
printf("Opened test schema file\n");
}

WJReader my_schema_reader;
my_schema_reader = WJROpenFILEDocument(FCGI_ToFILE(my_schema_file), NULL, 0);

if (my_schema_reader == NULL) {
printf("Failed to open test schema reader\n");
return 1;
} else {
printf("Opened test schema reader\n");
}

return 0;
}

关于c - 为什么链接到 FastCGI 库会导致段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20207357/

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