Using sessions with fastcgi++

1k Views Asked by At

I am having problems using sessions with fastcgi++ 2 beta.

Here is my code :

#include <fastcgi++/http.hpp>
#include <fastcgi++/request.hpp>
#include <fastcgi++/manager.hpp>
#include <string>

using namespace std;
using namespace Fastcgipp::Http;

class SessionsPage : public Fastcgipp::Request<char> {
    static Sessions<string> sessions;
    Sessions<string>::iterator session;

    bool response() {
        sessions.cleanup();

        out << "Content-Type: text/html;\r\n\r\n";
        out << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" << std::endl;
        out << "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"fr\" >" << std::endl;
        out << "    <head>" << std::endl;
        out << "        <title>Sessions</title>" << std::endl;
        out << "        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />" << std::endl;
        out << "    </head>" << std::endl;
        out << "    <body>" << std::endl;
        out << "        <p>Hello World!</p>" << std::endl;
        out << "    </body>" << std::endl;
        out << "</html>" << std::endl;
        return true;
    }
};

int main() {
    Fastcgipp::Manager<SessionsPage> fcgi;
    fcgi.handler();
}

I got the following error :

/tmp/ccRujo45.o: In function `SessionsPage::response()':
sessions_page.cpp:(.text._ZN12SessionsPage8responseEv[SessionsPage::response()]+0xd): undefined reference to `SessionsPage::sessions'
collect2: ld a retourné 1 code d'état d'exécution

If I comment this line, it compiles :

sessions.cleanup();

I am using this tutorial.

Thanks for your help.

1

There are 1 best solutions below

1
On BEST ANSWER

static Sessions<string> sessions; is only declared in the class; you need to create a definition for it. In this case I believe sticking Sessions<string> SessionsPage::sessions; after your class is fine, given that the class is used in only one translation unit. (If you use it in multiple translation units the static variable must be defined in one and only one translation unit)