Question

I want to write a website in C++11 using fcgi and nginx. At the moment only Clang++ combined with libc++ supports fully C++11.

But when I run my fcgi-program, I get a seg-fault when someone requests the page over the browser: It seems that the libc++ doesn't like how fcgi uses the streams.

The Test-Code:

#include <iostream>
#include <sstream>

#include "fcgio.h"


int main() {
    int count = 0;

    FCGX_Request request;

    FCGX_Init();
    FCGX_InitRequest(&request, 0, 0);

    while(FCGX_Accept_r(&request) == 0) {
        fcgi_streambuf cout_fcgi_streambuf(request.out);

        std::ostream fout(&cout_fcgi_streambuf);

        fout << "Content-type: text/html\r\n" <<
               "\r\n" <<
               "<title>CGI Hello!</title>" <<
               "<h1>CGI Hello!</h1>" <<
               "Request number" << ++count << "\n" << std::endl;
    }

    return 0;
}

The code above was compiled with:

clang++ -stdlib=libc++ -o index index.cpp -lfcgi++ -lfcgi -std=c++11 -g

gdb outputs the following:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000402bd9 in sputc (this=0x7fffffffe4d0, __c1=0, __c=10 '\n', __c2=4210300) at /usr/include/c++/v1/streambuf:351
351     *__nout_++ = __c;

If I compile it without -stdlib=libc++ everything works fine, except I can't use some c++11 features…

Is there a way I can run my fcgi-app without crashing and use libc++?

Was it helpful?

Solution

I had the exact same problem using the same set of tools.

As Dietmar Kühl pointed out, libfcgi++ was not compiled with libc++, and it was the problem for me. +1000 to him. Many thanks.

As a quick hacky test, I recompiled the latest stable libfcgi with the flag:

-stdlib=libc++

by running ./configure as usual, then editing two lines in the Makefile found in fcgi-dev-kit/libfcgi/Makefile:

CXX = clang++
# ....
CXXFLAGS = -g -O2 -std=c++0x -stdlib=libc++

and then running make in the top level directory.

Linking with the resulting libraries in fcgi-dev-kit/libfcgi/.libs/libfcgi++.a for example, fixed the segmentation faults.

The dev kit can be found here: http://www.fastcgi.com/drupal/node/5. You'll need to figure out a long term solution for linking with an appropriately compiled libfcgi++ if you need to use libc++, like I do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top