对 'FCGX' 的未定义引用

Undefined reference to '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

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

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 调用中。

此外,参数的顺序很重要,您的 .c 文件 必须 位于 -l 指令之前,即

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

是错的,对的是

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

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