簡體   English   中英

使用/ clr或clr:pure編譯時​​不支持Mutex(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 studio\2017\community\vc\tools\msvc\14.13.26128\include\mutex(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 studio\2017\community\vc\tools\msvc\14.13.26128\include\mutex(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:\Users\Laptop-attendance\source\repos\Timestamp\Timestamp\Testapi.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:%u\n", 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:%s\n", e.what());
        }

    }
    };
}

#endif /*TESTAPI_H*/

回答的最后 ,漢斯·帕桑特(Hans Passant)給出了一個提示,對您的情況很有用。 基本上,您需要一個單獨的c ++庫(關閉了cpprest支持),在其中包裝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 ,而后者不包含任何內容:

#include <restwrapper.h>
System::Void Testapi_Load(System::Object^  sender, System::EventArgs^  e) 
{
    RestWrapper wrapper;
    wrapper.test();
}

您可以從CLR項目中調用使用互斥鎖的類,您需要做的是創建一個普通的c ++項目並創建具有所需功能但不在其標頭中公開<mutex>的類,然后您可以自由調用這些類來自您的CLR項目。

只需右鍵單擊您的.cpp文件,然后在“常規->公共語言運行時支持”下選擇“無公共語言運行時支持”。

真正最簡單的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM