首页 > 解决方案 > 使用 cpprestsdk 在 linux 上编译 wstring 的问题

问题描述

我有这样的代码

using namespace web;                
using namespace http;   

const http_response& response = /*valid assignment*/
http_headers::const_iterator it = response.headers().find(L"SomeKey");
if (it != response.headers().end())
    {
        //doing something
    }

response有有效数据。它是用windows编译的。我想用 g++ 在 Linux 中编译相同的代码段。我该如何处理?我需要为编译添加一些标志吗?我收到这样的错误:

 error: no matching function for call to ‘web::http::http_headers::find(const wchar_t [17]) const’
   http_headers::const_iterator it = response.headers().find(L"SomeKey");
                                                                       ^

标签: c++linuxcpprest-sdk

解决方案


该项目wstring用于windows,string用于Linux。他们提供了一个类型string_t和一个宏U来帮助处理这个问题,您的代码需要更改才能在 Windows 和 Linux 上编译。

什么是实用程序::string_t 和“U”宏?C++ REST SDK 使用不同的字符串类型,具体取决于目标平台。例如,对于 Windows 平台,utility::string_t 是使用 UTF-16 的 std::wstring,在 Linux 上是使用 UTF-8 的 std::string。'U' 宏可用于创建平台类型的字符串文字。如果您使用的库导致与“U”宏发生冲突,例如 Boost.Iostreams,则可以通过在包含 C++ REST SDK 头文件之前定义宏“_TURN_OFF_PLATFORM_STRING”来关闭它。

请参阅此常见问题解答

一个典型的用法:

static const utility::string_t wl_birthday = U("wl.birthday");
static const utility::string_t wl_basic = U("wl.basic");

对于您的代码:

http_headers::const_iterator it = response.headers().find(U("SomeKey"));

警告此项目的状态

cpprestsdk 处于维护模式,我们不建议在新项目中使用它。我们将继续修复关键错误并解决安全问题。


推荐阅读