cpprestsdk icon indicating copy to clipboard operation
cpprestsdk copied to clipboard

cpprestsdk problems with eclipse linux

Open MilleniumBPO opened this issue 10 years ago • 2 comments

how to link cpprestsdk with eclipse in llinux ? i try to do it but i cant compile succesfully because some errors. I first followed the instructions for installation cpprest (https://github.com/Microsoft/cpprestsdk/wiki/How-to-build-for-Linux) , In C/C++ Build -> Settings I configure dialect c++11, add the link paths to includes and libs and add cpprest library , i work with gcc 5.4.1 , eclipse luna and ubuntu 14.04 , any suggestions ?

Note: I compile the same source in visual studio 2013 on windows 8.1 without problems.

the list of errors include:

cannot bind 'std::basic_ostream<wchar_t>' lvalue to 'std::basic_ostream<wchar_t>&&' C/C++ Problem invalid initialization of non-const reference of type 'web::json::value&' from an rvalue of type 'web::json::value' C/C++ Problem Description Resource Path Location Type no match for 'operator<<' (operand types are 'std::basic_ostream<wchar_t>' and 'const std::basic_string') C/C++ Problem no match for 'operator<<' (operand types are 'std::basic_ostream<wchar_t>' and 'std::basic_string') C/C++ Problem no matching function for call to'web::http::experimental::listener::http_listener::http_listener(const wchar_t [26])' C/C++ Problem.

The following code sample:

#include <cpprest/http_listener.h> #include <cpprest/json.h>

using namespace web; using namespace web::http; using namespace web::http::experimental::listener;

#include #include #include #include using namespace std;

#define TRACE(msg) wcout << msg #define TRACE_ACTION(a, k, v) wcout << a << L" (" << k << L", " << v << L")\n"

map<utility::string_t, utility::string_t> dictionary; typedef std::vector<std::pair<utility::string_t, json::value>> field_map; /* handlers implementation */

void handle_get(http_request request) { TRACE(L"\nhandle GET\n");

field_map answer;

for (auto const & p : dictionary)
{
    answer.push_back(make_pair(p.first, json::value(p.second)));
}

request.reply(status_codes::OK, json::value::object(answer));

}

void handle_request(http_request request, function<void(json::value &, field_map &)> action) { field_map answer;

request
    .extract_json()
    .then([&answer, &action](pplx::task<json::value> task) {
    try
    {
        auto & jvalue = task.get();

        if (!jvalue.is_null())
        {
            action(jvalue, answer);
        }
    }
    catch (http_exception const & e)
    {
        wcout << e.what() << endl;
    }
})
    .wait();

request.reply(status_codes::OK, json::value::object(answer));

}

void handle_post(http_request request) { TRACE("\nhandle POST\n");

handle_request(
    request,
    [](json::value & jvalue, field_map & answer)
{
    for (auto const & e : jvalue.as_array())
    {
        if (e.is_string())
        {
            auto key = e.as_string();

            auto pos = dictionary.find(key);
            if (pos == dictionary.end())
            {
                answer.push_back(make_pair(key, json::value(L"<nil>")));
            }
            else
            {
                answer.push_back(make_pair(pos->first, json::value(pos->second)));
            }
        }
    }
}
);

}

void handle_put(http_request request) { TRACE("\nhandle PUT\n");

handle_request(
    request,
    [](json::value & jvalue, field_map & answer)
{
    for (auto const & e : jvalue.as_object())
    {
        if (e.second.is_string())
        {
            auto key = e.first;
            auto value = e.second.as_string();

            if (dictionary.find(key) == dictionary.end())
            {
                TRACE_ACTION(L"added", key, value);
                answer.push_back(make_pair(key, json::value(L"<put>")));
            }
            else
            {
                TRACE_ACTION(L"updated", key, value);
                answer.push_back(make_pair(key, json::value(L"<updated>")));
            }

            dictionary[key] = value;
        }
    }
}
);

}

void handle_del(http_request request) { TRACE("\nhandle DEL\n");

handle_request(
    request,
    [](json::value & jvalue, field_map & answer)
{
    set<utility::string_t> keys;
    for (auto const & e : jvalue.as_array())
    {
        if (e.is_string())
        {
            auto key = e.as_string();

            auto pos = dictionary.find(key);
            if (pos == dictionary.end())
            {
                answer.push_back(make_pair(key, json::value(L"<failed>")));
            }
            else
            {
                TRACE_ACTION(L"deleted", pos->first, pos->second);
                answer.push_back(make_pair(key, json::value(L"<deleted>")));
                keys.insert(key);
            }
        }
    }

    for (auto const & key : keys)
        dictionary.erase(key);
}
);

}

int main() { http_listener listener(L"http://localhost/restdemo");

listener.support(methods::GET, handle_get);
listener.support(methods::POST, handle_post);
listener.support(methods::PUT, handle_put);
listener.support(methods::DEL, handle_del);

try
{
    listener
        .open()
        .then([&listener]() {TRACE(L"\nstarting to listen\n"); })
        .wait();

    while (true);
}
catch (exception const & e)
{
    wcout << e.what() << endl;
}

return 0;

}

MilleniumBPO avatar Nov 02 '16 20:11 MilleniumBPO

Generally, anywhere in your above code that you pass a string into a cpprestsdk API like L"hello, world!" needs to use our U() macro instead: U("hello, world!"). This will convert the strings into const wchar_t* on Windows, but use const char* on Linux.

ras0219-msft avatar Nov 03 '16 02:11 ras0219-msft

@ras0219-msft, I'm also running into this problem. I don't understand why cpprest has to use char* in Linux while wchar_t* in Windows. This makes us difficult writing platform independent code with cpprest. I would appreciate it if you could deliberate a little bit of the reason behind this.

bubbercn avatar May 26 '17 06:05 bubbercn