Search code examples
cubuntufastcgi

Undefined reference to 'FCGX'


I am trying to compile this program

#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;
    }

With this command

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

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.


Solution

  • 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.

    No, this is not due to header files not being found. These are linker errors; they happen after your file succesfully was compiled. You're not linking against all necessary libraries.

    You'll have to figure out which library contains FCGX_Init and add that as -l<library> to your GCC call.

    Also, order of arguments is important, and your .c file must come before your -l directives, ie.

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

    is wrong, right would be

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

    Also, you should never compile code as root (do not use sudo for building software ever).