Visual Studio:在使用cpprestsdk创建dll时找不到命名空间



我想创建一个可以调用一些api并返回响应的dll文件(这将在Unity项目中使用)。

为了让事情变得更容易,我安装了cpprestsdk并遵循了这个简单的教程,它工作得很好。

但是当我试图改变我的代码以便我可以创建一个dll时,我开始得到编译错误。

下面是我的源代码-

#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include "pch.h"
using namespace utility;                    // Common utilities like string conversions
using namespace web;                        // Common features like URIs.
using namespace web::http;                  // Common HTTP functionality
using namespace web::http::client;          // HTTP client features
using namespace concurrency::streams;       // Asynchronous streams
extern "C" {
void SendRequest() {
auto fileStream = std::make_shared<ostream>();
// Open stream to output file.
pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)
{
*fileStream = outFile;
// Create http_client to send the request.
http_client client(U("https://www.bing.com/"));
// Build request URI and start the request.
uri_builder builder(U("/search"));
builder.append_query(U("q"), U("cpprestsdk github"));
return client.request(methods::GET, builder.to_string());
})
// Handle response headers arriving.
.then([=](http_response response)
{
printf("Received response status code:%un", response.status_code());
// Write response body into the file.
return response.body().read_to_end(fileStream->streambuf());
})
// Close the file stream.
.then([=](size_t)
{
return fileStream->close();
});
// Wait for all the outstanding I/O to complete and handle any exceptions
try
{
requestTask.wait();
}
catch (const std::exception& e)
{
printf("Error exception:%sn", e.what());
}
}
}

和我的头文件-

#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#define SENDREQUEST_API __declspec(dllexport) 
extern "C" {
SENDREQUEST_API void SendRequest();
}

和我得到的错误(截断)-

1>C:Users91805sourcereposRestAPICallsRestAPICallsSendRequest.cpp(5,24): error C2871: 'utility': a namespace with this name does not exist
1>C:Users91805sourcereposRestAPICallsRestAPICallsSendRequest.cpp(6,20): error C2871: 'web': a namespace with this name does not exist
1>C:Users91805sourcereposRestAPICallsRestAPICallsSendRequest.cpp(7,17): error C2653: 'web': is not a class or namespace name

如何解决以上错误?

PS:我正在使用Visual Studio创建dll(s),并能够成功创建一个不包含外部依赖关系的dll(从这里开始)

感谢

如果我把所有的代码片段放在头文件中,那就没有问题了。

#pragma once
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include "pch.h"
using namespace utility;                    // Common utilities like string conversions
using namespace web;                        // Common features like URIs.
using namespace web::http;                  // Common HTTP functionality
using namespace web::http::client;          // HTTP client features
using namespace concurrency::streams;
//extern "C" {
//    SENDREQUEST_API void SendRequest();
//}
#define SENDREQUEST_API __declspec(dllexport) 
extern "C" {
SENDREQUEST_API void SendRequest();
}
extern "C" {
void SendRequest() {
auto fileStream = std::make_shared<ostream>();
// Open stream to output file.
pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)
{
*fileStream = outFile;
// Create http_client to send the request.
http_client client(U("https://www.bing.com/"));
// Build request URI and start the request.
uri_builder builder(U("/search"));
builder.append_query(U("q"), U("cpprestsdk github"));
return client.request(methods::GET, builder.to_string());
})
// Handle response headers arriving.
.then([=](http_response response)
{
printf("Received response status code:%un", response.status_code());
// Write response body into the file.
return response.body().read_to_end(fileStream->streambuf());
})
// Close the file stream.
.then([=](size_t)
{
return fileStream->close();
});
// Wait for all the outstanding I/O to complete and handle any exceptions
try
{
requestTask.wait();
}
catch (const std::exception& e)
{
printf("Error exception:%sn", e.what());
}
}
}

最新更新