【问题标题】:Undefined reference to 'FCGX' [duplicate]对'FCGX'的未定义引用[重复]
【发布时间】:2016-10-11 13:26:36
【问题描述】:

我正在尝试编译这个程序

#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

我认为是因为找不到头文件。

【问题讨论】:

    标签: c ubuntu fastcgi


    【解决方案1】:

    然后我得到这个错误:

    /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
    

    我认为是因为找不到头文件。

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

    您必须弄清楚哪个库包含FCGX_Init,并将其作为-l&lt;library&gt; 添加到您的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 构建软件)。

    【讨论】:

    • 感谢您的回答!我完全摆脱了“-I”论点,有点想通了。它编译没有错误!
    • 嗯,这可能会起作用,因为您的服务器运行时会从您的 fcgi 库中加载符号。
    猜你喜欢
    • 1970-01-01
    • 2011-07-27
    • 2013-06-20
    • 2012-02-05
    • 2014-12-27
    • 2012-11-15
    • 2022-01-16
    • 2017-01-26
    相关资源
    最近更新 更多