使用 /clr 或 clr:pure (cpprestsdk aka casablanca) 编译时不支持互斥
Mutex is not supported when compiling with /clr or clr:pure (cpprestsdk aka casablanca)
我在visual c++
中创建了一个64 bit
配置的CLR project
,并尝试使用cpprestsdk aka casablanca 64bit
.
但是当我运行该项目时,发生了错误:
1>------ Build started: Project: Timestamp, Configuration: Debug x64 ------
1>MyForm.cpp
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.13.26128includemutex(8): fatal error C1189: #error: <mutex> is not supported when compiling with /clr or /clr:pure.
1>Testapi.cpp
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.13.26128includemutex(8): fatal error C1189: #error: <mutex> is not supported when compiling with /clr or /clr:pure.
1>Generating Code...
1>Done building project "Timestamp.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
其他错误:
E2093 a local lambda is not allowed in a member function of a managed class Timestamp c:UsersLaptop-attendancesourcereposTimestampTimestampTestapi.h 97
IDE 显示有关.then
函数中'['
字符的错误,例如 .then([=](http_response response)
,如果您指出它,它会说"a local lambda is not allowed in a member function of a managed class"
如果我在具有 64 位配置的 Visual c ++ Windows Console Application
中尝试该cpprestsdk
,它可以正常工作。
我正在使用Visual Studio 2017。
你认为cpprestsdk不能用在vc++的CLR项目中吗?
谢谢。
这是我的代码,以及我刚刚从教程中得到的关于cpprestsdk的代码:
#ifndef TESTAPI_H
#define TESTAPI_H
#pragma once
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <cpprest/json.h>
namespace Timestamp {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
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
/// <summary>
/// Summary for Testapi
/// </summary>
public ref class Testapi : public System::Windows::Forms::Form
{
public:
Testapi(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Testapi()
{
if (components)
{
delete components;
}
}
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->SuspendLayout();
//
// Testapi
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 261);
this->Name = L"Testapi";
this->Text = L"Testapi";
this->Load += gcnew System::EventHandler(this, &Testapi::Testapi_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Testapi_Load(System::Object^ sender, System::EventArgs^ e) {
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("http://13.231.231.252:3000/api/individual_employment_setting/detail/172"));
// 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);
})
// 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());
stringstreambuf buffer;
response.body().read_to_end(buffer).get();
//show content in console
printf("Response body: n %s", buffer.collection().c_str());
//parse content into a JSON object:
//json::value jsonvalue = json::value::parse(buffer.collection());
return fileStream->print(buffer.collection()); //write to file anyway
})
// 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());
}
}
};
}
#endif /*TESTAPI_H*/
在这个答案的最后,Hans Passant给出了一个提示,这对你来说很有用。基本上,您需要一个单独的 c++ 库(关闭 clr 支持(,您可以在其中包装cpprest
代码,从 CLR 项目链接此库,并确保没有包含的标头会带来<mutex>
。
举个例子,在 restwrapper.h 头文件中有一个这样的类:
class RestWrapper
{
public:
void test();
};
在其实现文件中,restwrapper.cpp:
#include "restwrapper.h"
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <cpprest/json.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
void RestWrapper::test()
{
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("http://13.231.231.252:3000/api/individual_employment_setting/detail/172"));
//etc ...
}
可以在 DLL 中编译此类(链接cpprest
及其所有相关库(,然后使 CLR 项目链接该库。在 CLR 项目中,您只需要包含 restwrapper.h,而 restwrapper.h 又不包含任何内容:
#include <restwrapper.h>
System::Void Testapi_Load(System::Object^ sender, System::EventArgs^ e)
{
RestWrapper wrapper;
wrapper.test();
}
您可以从 CLR 项目中调用使用互斥锁的类,您需要做的是创建一个普通的 c++ 项目并创建具有所需功能但不在其标头中公开<mutex>
的类,然后您可以自由地从 CLR 项目调用这些类。
只需右键单击您的.cpp文件,然后在"常规 -> 公共语言运行时支持"下选择"无公共语言运行时支持"。
真的是最简单的解决方案。
- 二叉排序树无法编译
- 编译时未启用intel oneApi CUDA支持
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++项目编译强制使用 /clr 选项,尽管在没有公共语言运行时支持的情况下执行它
- 在我的 CLR 项目中添加包含会导致无法在启用 /clr 的情况下编译 WLR
- <mutex> 使用 /clr 或 /clr:pure 编译时不支持
- 使用 /clr 或 clr:pure (cpprestsdk aka casablanca) 编译时不支持互斥
- 我应该如何使用SQL Server CLR来使用由C++ / C编译的UDT
- C++/CLR:仅在编译 x86 时出现命令行错误
- 当未选择 /CLR时,编译的MATLAB代码崩溃
- 用CLR编译的本机DLL,在使用它们之前,我必须公开这些函数吗
- 在 Visual Studio 2012 中编译启用 CLR C++项目时出现 C2248 错误
- C++CLR不会在调试之外进行编译
- 在c#项目中,如何引用用c++编写并使用/clr编译的项目?
- CFileException::sharing使用编译的 /clr 打开可执行文件时出现冲突
- 将本机编译的c++ DLL添加到CLR c++项目中
- 如何使c++ fstream类引用在Visual Studio 2010 CLR中编译?
- 我在Visual Studio 2012 Pro中有一个C++DLL类型的项目,但我无法将其编译为CLR - 为什么会这样?
- 收到此错误时如何实现非托管线程安全集合: <mutex> 使用 /clr 编译时不支持
- 使用/clr选项编译C代码