2

Mongoose ライブラリを使用して C++ OOP で API サーバーを開発しています。 embServer.h

   class embServer
{

private:
    constexpr static const char *s_http_port = "8000";
    constexpr static struct mg_serve_http_opts s_http_server_opts = { .document_root = "web_root" };  // C99;
    static int s_sig_num;
    constexpr static const struct mg_str s_get_method = MG_MK_STR("GET");
    constexpr static const struct mg_str s_put_method = MG_MK_STR("PUT");
    constexpr static const struct mg_str s_delele_method = MG_MK_STR("DELETE");


public:
    embServer();
    ~embServer();

    Database dbConn;
    //int embServer::s_sig_num = 0;


    static void signal_handler(int sig_num)
    {
        signal(sig_num, signal_handler);
        s_sig_num = sig_num;
    }

    static int has_prefix(const struct mg_str *uri, const struct mg_str *prefix)
    {
        return uri->len > prefix->len && memcmp(uri->p, prefix->p, prefix->len) == 0;
    }

    static int is_equal(const struct mg_str *s1, const struct mg_str *s2)
    {
        return s1->len == s2->len && memcmp(s1->p, s2->p, s2->len) == 0;
    }

    static void ev_handler(struct mg_connection *nc, int ev, void *ev_data)
    {
        static const struct mg_str api_prefix = MG_MK_STR("/api/v1");
        struct http_message *hm = (struct http_message *) ev_data;
        struct mg_str key;

        switch (ev)
        {
        case MG_EV_HTTP_REQUEST:

            if (has_prefix(&hm->uri, &api_prefix))
            {
                key.p = hm->uri.p + api_prefix.len;

                key.len = hm->uri.len - api_prefix.len;

                if (embServer::is_equal(&hm->method, &s_get_method))
                {
                    //dbConn.db_op(nc, hm, &key, 1);
                }
                else if (embServer::is_equal(&hm->method, &s_put_method))
                {
                    //dbConn.db_op(nc, hm, &key, 2);
                }
                else if (embServer::is_equal(&hm->method, &s_delele_method))
                {
                    //dbConn.db_op(nc, hm, &key, 3);
                }
                else
                {
                    mg_printf(nc, "%s","HTTP/1.0 501 Not Implemented\r\n" "Content-Length: 0\r\n\r\n");
                }
            }
            else
            {
                mg_serve_http(nc, hm, &s_http_server_opts); /* Serve static content */
           }
            break;
        default:
            break;
        }
    }

    void Run()
    {

        struct mg_mgr mgr;
        struct mg_connection *nc;
        //int i;

        /* Open listening socket */
        mg_mgr_init(&mgr, NULL);
        nc = mg_bind(&mgr, s_http_port, embServer::ev_handler);
        mg_set_protocol_http_websocket(nc);


        /* For each new connection, execute ev_handler in a separate thread */
        mg_enable_multithreading(nc);



        /**
        for (i = 1; i < argc; i++)
        {
            if (strcmp(argv[i], "-D") == 0)
            {
                mgr.hexdump_file = argv[++i];
            }
            else if (strcmp(argv[i], "-f") == 0)
            {
                s_db_path = argv[++i];
            }
            else if (strcmp(argv[i], "-r") == 0)
            {
                s_http_server_opts.document_root = argv[++i];
            }
        }
        Parse command line arguments */

        signal(SIGINT, &embServer::signal_handler);
        signal(SIGTERM, &embServer::signal_handler);

        /* Run event loop until signal is received */
        printf("Starting RESTful server on port %s\n", s_http_port);
        while (s_sig_num == 0)
        {
            mg_mgr_poll(&mgr, 1000);
        }

        /* Cleanup */
        mg_mgr_free(&mgr);
        printf("Exiting on signal %d\n", s_sig_num);


    }
 };
 int embServer::s_sig_num = 0;

エラー表示

    `F:\PROJECTS\CP\apiCrud\embServer.h|48|warning: variable 'key' set but not used [-Wunused-but-set-variable]|
obj\Debug\main.o||In function `ZN9embServer10ev_handlerEP13mg_connectioniPv':|
F:\PROJECTS\CP\apiCrud\embServer.h|79|undefined reference to `embServer::s_http_server_opts'|
F:\PROJECTS\CP\apiCrud\embServer.h|79|undefined reference to `embServer::s_http_server_opts'|
F:\PROJECTS\CP\apiCrud\embServer.h|79|undefined reference to `embServer::s_http_server_opts'|
F:\PROJECTS\CP\apiCrud\embServer.h|79|undefined reference to `embServer::s_http_server_opts'|
F:\PROJECTS\CP\apiCrud\embServer.h|79|undefined reference to `embServer::s_http_server_opts'|
obj\Debug\main.o:F:\PROJECTS\CP\apiCrud\embServer.h|79|more undefined references to `embServer::s_http_server_opts' follow|
obj\Debug\main.o||In function `main':|
F:\PROJECTS\CP\apiCrud\main.cpp|15|undefined reference to `embServer::embServer()'|
F:\PROJECTS\CP\apiCrud\main.cpp|15|undefined reference to `embServer::~embServer()'|
F:\PROJECTS\CP\apiCrud\main.cpp|15|undefined reference to `embServer::~embServer()'|
||error: ld returned 1 exit status|
||=== Build failed: 10 error(s),(0 minute(s), 22 second(s)) ===|`

int main(int argc, char *argv[])
{
    embServer emb;
    emb.Run();

    return 0;
}
4

1 に答える 1

0

コンパイラは、これらの変更を行う必要があることを直接伝えています。

  1. に追加constexprstatic struct mg_serve_http_opts s_http_server_optsます。
  2. から を削除&mg_serve_http(nc, hm, &s_http_server_opts)ます。
于 2016-05-16T10:05:15.603 に答える