2016-07-11 101 views
0

我試圖編譯在G ++以下文件:嘗試編譯websocketpp端點在G ++

Test.hpp

#include <websocketpp/client.hpp> 
#include <websocketpp/config/asio_client.hpp> 

typedef websocketpp::client<websocketpp::config::asio_tls_client> client; 

using websocketpp::lib::bind; 

typedef websocketpp::config::asio_tls_client::message_type::ptr message_ptr; 
typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr; 
typedef client::connection_ptr connection_ptr; 

class ClientConnection { 
public: 
    ClientConnection(); 

    void start(std::string uri); 

    // Event handlers 
    context_ptr on_tls_init(websocketpp::connection_hdl); 
    void on_fail(websocketpp::connection_hdl hdl); 
    void on_open(websocketpp::connection_hdl hdl); 
    void on_message(websocketpp::connection_hdl &hdl, message_ptr message); 
    void on_close(websocketpp::connection_hdl); 

private: 
    client c; 
}; 

Test.cpp的:

#include <iostream> 

#include "Test.hpp" 

ClientConnection::ClientConnection() { 
    // Reset the log channels 
    c.clear_access_channels(websocketpp::log::alevel::all); 

    c.set_access_channels(websocketpp::log::alevel::app | websocketpp::log::alevel::connect); 
    c.set_error_channels(websocketpp::log::elevel::all); 

    // Initialize ASIO 
    c.init_asio(); 

    // Bind handlers 
    c.set_tls_init_handler(bind<context_ptr>(
     &ClientConnection::on_tls_init, 
     this, 
     websocketpp::lib::placeholders::_1 
    )); 
    c.set_message_handler(bind(
     &ClientConnection::on_message, 
     this, 
     websocketpp::lib::placeholders::_1, 
     websocketpp::lib::placeholders::_2 
    )); 
    c.set_open_handler(bind(
     &ClientConnection::on_open, 
     this, 
     websocketpp::lib::placeholders::_1 
    )); 
    c.set_close_handler(bind(
     &ClientConnection::on_close, 
     this, 
     websocketpp::lib::placeholders::_1 
    )); 
    c.set_fail_handler(bind(
     &ClientConnection::on_fail, 
     this, 
     websocketpp::lib::placeholders::_1 
    )); 
} 

// Open a connection to the URI provided 
void ClientConnection::start(std::string uri) { 
    websocketpp::lib::error_code ec; 
    client::connection_ptr con = c.get_connection(uri, ec); 

    if (ec) { // failed to create connection 
     c.get_alog().write(websocketpp::log::alevel::app, ec.message()); 
     return; 
    } 

    // Open the connection 
    c.connect(con); 
    c.run(); 
} 

context_ptr ClientConnection::on_tls_init(websocketpp::connection_hdl) { 
    context_ptr ctx = std::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::sslv23); 

    try { 
     ctx->set_options(boost::asio::ssl::context::default_workarounds | 
      boost::asio::ssl::context::no_sslv2 | 
      boost::asio::ssl::context::no_sslv3 | 
      boost::asio::ssl::context::single_dh_use); 
    } 
    catch (std::exception& e) { 
     std::cout << "Error in context pointer: " << e.what() << std::endl; 
    } 
    return ctx; 
} 

void ClientConnection::on_fail(websocketpp::connection_hdl hdl) { 
    client::connection_ptr con = c.get_con_from_hdl(hdl); 

    // Print as much information as possible 
    c.get_elog().write(websocketpp::log::elevel::warn, 
     "Fail handler: \n" + 
     std::to_string(con->get_state()) + "\n" + 
     std::to_string(con->get_local_close_code()) + "\n" + 
     con->get_local_close_reason() + "\n" + 
     std::to_string(con->get_remote_close_code()) + "\n" + 
     con->get_remote_close_reason() + "\n" + 
     std::to_string(con->get_ec().value()) + " - " + con->get_ec().message() + "\n"); 
} 

void ClientConnection::on_open(websocketpp::connection_hdl hdl) { 
} 

void ClientConnection::on_message(websocketpp::connection_hdl &hdl, message_ptr message) { 
    std::cout << "<< " << message->get_payload() << std::endl; 
} 

void ClientConnection::on_close(websocketpp::connection_hdl) { 
    std::cout << "Closed." << std::endl; 
} 

然而,它在嘗試編譯時會引發以下錯誤(包括g ++編譯命令):

> g++ Test.cpp -o Test.o -I./ -I../../lib/websocketpp -I~/boost_1_61_0 -L/usr/local/lib -I/usr/local/include -lpthread -lboost_system -lboost_random -lboost_timer -lboost_chrono -lrt -lssl -lcrypto -std=c++14 

Test.cpp: In constructor ‘ClientConnection::ClientConnection()’: 
Test.cpp:26:3: error: no matching function for call to ‘websocketpp::client<websocketpp::config::asio_tls_client>::set_message_handler(std::_Bind_helper<false, void (ClientConnection::*)(std::weak_ptr<void>&, std::shared_ptr<websocketpp::message_buffer::message<websocketpp::message_buffer::alloc::con_msg_manager> >), ClientConnection*, const std::_Placeholder<1>&, const std::_Placeholder<2>&>::type)’ 
)); 
^
Test.cpp:26:3: note: candidate is: 
In file included from ../../lib/websocketpp/websocketpp/roles/client_endpoint.hpp:31:0, 
       from ../../lib/websocketpp/websocketpp/client.hpp:31, 
       from Test.hpp:1, 
       from Test.cpp:3: 
../../lib/websocketpp/websocketpp/endpoint.hpp:322:10: note: void websocketpp::endpoint<connection, config>::set_message_handler(websocketpp::endpoint<connection, config>::message_handler) [with connection = websocketpp::connection<websocketpp::config::asio_tls_client>; config = websocketpp::config::asio_tls_client; websocketpp::endpoint<connection, config>::message_handler = std::function<void(std::weak_ptr<void>, std::shared_ptr<websocketpp::message_buffer::message<websocketpp::message_buffer::alloc::con_msg_manager> >)>] 
    void set_message_handler(message_handler h) { 
     ^
../../lib/websocketpp/websocketpp/endpoint.hpp:322:10: note: no known conversion for argument 1 from ‘std::_Bind_helper<false, void (ClientConnection::*)(std::weak_ptr<void>&, std::shared_ptr<websocketpp::message_buffer::message<websocketpp::message_buffer::alloc::con_msg_manager> >), ClientConnection*, const std::_Placeholder<1>&, const std::_Placeholder<2>&>::type {aka std::_Bind<std::_Mem_fn<void (ClientConnection::*)(std::weak_ptr<void>&, std::shared_ptr<websocketpp::message_buffer::message<websocketpp::message_buffer::alloc::con_msg_manager> >)>(ClientConnection*, std::_Placeholder<1>, std::_Placeholder<2>)>}’ to ‘websocketpp::endpoint<websocketpp::connection<websocketpp::config::asio_tls_client>, websocketpp::config::asio_tls_client>::message_handler {aka std::function<void(std::weak_ptr<void>, std::shared_ptr<websocketpp::message_buffer::message<websocketpp::message_buffer::alloc::con_msg_manager> >)>}’ 

我確定我做錯了什麼,但不知道是什麼。任何幫助感謝!我是在Linux下編譯C++的新手,所以很可能我在編譯/鏈接錯誤或其他東西。

回答

0

更改:

on_message(websocketpp::connection_hdl &hdl, message_ptr message) 

到:

on_message(websocketpp::connection_hdl hdl, message_ptr message) 

固定它。

但是之前的代碼在Visual Studio C++下編譯。有誰知道爲什麼發生這種情況?

+1

原因是,Visual Studio中一個長期存在的bug /「編譯器擴展」:http://stackoverflow.com/questions/16380966/non-const-reference-bound-to-temporary-visual-studio-bug –

+0

感謝您的信息。 – Jack