2016-06-10 248 views
0

我试图编译这个程序未定义参考 'FCGX'

#include <stdlib.h> 
#include <string.h> 
#include <syslog.h> 
#include <alloca.h> 
#include <fcgiapp.h> 

#define LISTENSOCK_FILENO 0 
#define LISTENSOCK_FLAGS 0 

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

    openlog("testfastcgi", LOG_CONS|LOG_NDELAY, LOG_USER); 

    int err = FCGX_Init(); 
    /* call before Accept in multithreaded apps */ 
    if (err) { 
     syslog (LOG_INFO, "FCGX_Init failed: %d", err); 
     return 1; 
     } 

    FCGX_Request cgi; 
    err = FCGX_InitRequest(&cgi, LISTENSOCK_FILENO, LISTENSOCK_FLAGS); 
    if (err) { 
     syslog(LOG_INFO, "FCGX_InitRequest failed: %d", err); 
     return 2; 
     } 

    while (1) { 
    err = FCGX_Accept_r(&cgi); 
    if (err) { 
     syslog(LOG_INFO, "FCGX_Accept_r stopped: %d", err); 
     break; 
     } 
    char** envp; 
    int size = 200; 
    for (envp = cgi.envp; *envp; ++envp) 
     size += strlen(*envp) + 11; 

    return 0; 
    } 

使用此命令

sudo gcc -I/usr/local/include -lfcgi fastcgi.c -o test.fastcgi 

然后我得到这个错误:

/tmp/ccsqpUeQ.o: In function 

fastcgi. :(.text+0x3d): undefined reference to `FCGX_Init' 
fastcgi. :(.text+0x88): undefined reference to `FCGX_InitRequest' 
fastcgi. :(.text+0xc9): undefined reference to `FCGX_Accept_r' 
fastcgi. :(.text+0x373): undefined reference to `FCGX_PutStr' 
collect2: error: ld returned 1 exit status 

我想这是因为没有找到头文件。

回答

0

I then get this error:

/tmp/ccsqpUeQ.o: In function 
fastcgi. :(.text+0x3d): undefined reference to `FCGX_Init' 
fastcgi. :(.text+0x88): undefined reference to `FCGX_InitRequest' 
fastcgi. :(.text+0xc9): undefined reference to `FCGX_Accept_r' 
fastcgi. :(.text+0x373): undefined reference to `FCGX_PutStr' 
collect2: error: ld returned 1 exit status 

I think it's because the header files aren't being found.

不,这不是由于找不到头文件造成的。这些是链接器错误;它们在您的文件成功编译后发生。你没有链接所有必要的库。

您必须弄清楚哪个库包含FCGX_Init并将其作为-l<library>添加到您的GCC调用中。

而且,参数顺序很重要,你的-l指令,即在你.c文件必须来。

gcc -I/usr/local/include -lfcgi fastcgi.c -o test.fastcgi 

是错误的,正确的是

gcc -I/usr/local/include fastcgi.c -lfcgi -o test.fastcgi 

此外,你应该永远编译代码作为root(不使用sudo永远构建软件)。

+0

谢谢你的回答!我有点想通过完全摆脱'我'的论点。它编译没有错误! –

+0

对啊,这可能会起作用,因为你的服务器运行时加载你的fcgi库中的符号。 –