2013-11-26 72 views
0

我有一個程序使用一個名爲「wjelement」的庫,每當我嘗試在FastCGI中使用這個庫時,我都會遇到一個段錯誤。下面我做了一個簡化的測試用例。如果我在沒有fcgi_stdio.h的情況下編譯代碼,並且不鏈接到庫,代碼工作正常,如果我添加fastcgi頭並鏈接它,即使我沒有使用任何快速cgi調用,我也會收到段錯誤。爲什麼鏈接到FastCGI庫會導致段錯誤?

在我的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 
+2

也許其中一個涉及的頭文件重新定義FILE? –

+0

是,那就是:P –

回答

2

在這裏找到了答案: http://www.fastcgi.com/devkit/doc/fcgi-devel-kit.htm

如果您的應用程序將FILE *傳遞給您沒有酸的庫中實現的函數CE代碼,那麼你就需要包括這些庫的頭文件中包括fcgi_stdio.h

在此之前,我不得不從FCGI_FILE *轉換爲FILE *與FCGI_ToFILE(FCGI_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; 
} 
相關問題