簡體   English   中英

使用HTTP客戶端主體時帶有Boost Mac OSX seg的cpp-Netlib錯誤

[英]cpp-Netlib with Boost Mac OSX seg fault when using HTTP Client body

我最近開始學習cpp-netlib,它需要Boost,並且嘗試從該站點http://cpp-netlib.org/0.11.2/examples/http/http_client編譯其中一個cpp-netlib示例時遇到了一些問題。 html 我以前從未使用過Boost,並且想知道在編譯程序時應該如何確定要鏈接的內容? 我目前使用自制軟件作為我的軟件包管理器。 我通過運行安裝了boost

brew install boost --c++11

然后我通過運行安裝cpp-netlib

brew install cpp-netlib

我從站點復制並粘貼了代碼示例,並對請求URI進行了硬編碼

#include <boost/network/protocol/http/client.hpp>
#include <iostream>
int main()
{
    using namespace boost::network;
    http::client client;
    http::client::request request("http://www.boost.org");
    request << header("Connection", "close");
    http::client::response response = client.get(request);
    std::cout << body(response) << std::endl;
    return 0;
}

在與室友進行Google數小時搜索后,我們找到了需要鏈接的增強庫,我們還發現clang ++會發現我的Macs舊版本的openssl已預先安裝在操作系統中,而不是我通過自制軟件安裝的新版本。 有了這些新知識,我們發現使用此命令進行編譯是可行的

clang++ -g -I /usr/local/Cellar/openssl/1.0.2e/include test.cpp -L /usr/local/Cellar/openssl/1.0.2e/lib -lboost_system-mt -lboost_thread-mt -lcppnetlib-client-connections -lcppnetlib-uri -lcppnetlib-server-parsers -lssl -lcrypto

我現在在運行程序時遇到分段錯誤。 當我嘗試運行lldb調試我的程序時,lldb退出,並顯示“總線錯誤:10”。 我發現運行此行代碼時發生seg錯誤

std::cout << body(response) << std::endl;

當我運行valgrind時,我得到以下輸出

==1123== Memcheck, a memory error detector
==1123== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==1123== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==1123== Command: ./a.out
==1123== 
--1123-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option
--1123-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 2 times)
--1123-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 4 times)
--1123-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 8 times)
==1123== Thread 2:
==1123== Jump to the invalid address stated on the next line
==1123==    at 0x0: ???
==1123==    by 0x100016A5A: boost::asio::detail::task_io_service::do_run_one(boost::asio::detail::scoped_lock<boost::asio::detail::posix_mutex>&, boost::asio::detail::task_io_service_thread_info&, boost::system::error_code const&) (task_io_service.ipp:372)
==1123==    by 0x10001669A: boost::asio::detail::task_io_service::run(boost::system::error_code&) (task_io_service.ipp:149)
==1123==    by 0x10000FDD0: boost::asio::io_service::run() (io_service.ipp:59)
==1123==    by 0x1000196D1: boost::_mfi::mf0<unsigned long, boost::asio::io_service>::operator()(boost::asio::io_service*) const (in ./a.out)
==1123==    by 0x100019620: unsigned long boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> >::operator()<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list0>(boost::_bi::type<unsigned long>, boost::_mfi::mf0<unsigned long, boost::asio::io_service>&, boost::_bi::list0&, long) (in ./a.out)
==1123==    by 0x10001959B: boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> > >::operator()() (in ./a.out)
==1123==    by 0x10001874B: boost::detail::thread_data<boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> > > >::run() (thread.hpp:116)
==1123==    by 0x1003ADFD5: boost::(anonymous namespace)::thread_proxy(void*) (in /usr/local/Cellar/boost/1.59.0/lib/libboost_thread-mt.dylib)
==1123==    by 0x100B289B0: _pthread_body (in /usr/lib/system/libsystem_pthread.dylib)
==1123==    by 0x100B2892D: _pthread_start (in /usr/lib/system/libsystem_pthread.dylib)
==1123==    by 0x100B26384: thread_start (in /usr/lib/system/libsystem_pthread.dylib)
==1123==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==1123== 
==1123== 
==1123== Process terminating with default action of signal 11 (SIGSEGV)
==1123==  Bad permissions for mapped region at address 0x0
==1123==    at 0x0: ???
==1123==    by 0x100016A5A: boost::asio::detail::task_io_service::do_run_one(boost::asio::detail::scoped_lock<boost::asio::detail::posix_mutex>&, boost::asio::detail::task_io_service_thread_info&, boost::system::error_code const&) (task_io_service.ipp:372)
==1123==    by 0x10001669A: boost::asio::detail::task_io_service::run(boost::system::error_code&) (task_io_service.ipp:149)
==1123==    by 0x10000FDD0: boost::asio::io_service::run() (io_service.ipp:59)
==1123==    by 0x1000196D1: boost::_mfi::mf0<unsigned long, boost::asio::io_service>::operator()(boost::asio::io_service*) const (in ./a.out)
==1123==    by 0x100019620: unsigned long boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> >::operator()<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list0>(boost::_bi::type<unsigned long>, boost::_mfi::mf0<unsigned long, boost::asio::io_service>&, boost::_bi::list0&, long) (in ./a.out)
==1123==    by 0x10001959B: boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> > >::operator()() (in ./a.out)
==1123==    by 0x10001874B: boost::detail::thread_data<boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> > > >::run() (thread.hpp:116)
==1123==    by 0x1003ADFD5: boost::(anonymous namespace)::thread_proxy(void*) (in /usr/local/Cellar/boost/1.59.0/lib/libboost_thread-mt.dylib)
==1123==    by 0x100B289B0: _pthread_body (in /usr/lib/system/libsystem_pthread.dylib)
==1123==    by 0x100B2892D: _pthread_start (in /usr/lib/system/libsystem_pthread.dylib)
==1123==    by 0x100B26384: thread_start (in /usr/lib/system/libsystem_pthread.dylib)
==1123== 
==1123== HEAP SUMMARY:
==1123==     in use at exit: 150,091 bytes in 3,656 blocks
==1123==   total heap usage: 3,912 allocs, 256 frees, 194,518 bytes allocated
==1123== 
==1123== LEAK SUMMARY:
==1123==    definitely lost: 400 bytes in 4 blocks
==1123==    indirectly lost: 80,493 bytes in 2,673 blocks
==1123==      possibly lost: 0 bytes in 0 blocks
==1123==    still reachable: 47,160 bytes in 790 blocks
==1123==         suppressed: 22,038 bytes in 189 blocks
==1123== Rerun with --leak-check=full to see details of leaked memory
==1123== 
==1123== For counts of detected and suppressed errors, rerun with: -v
==1123== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Killed: 9

任何幫助深表感謝!

我發現要擺脫分段錯誤,我需要包含-std = c ++ 11來進行命令編譯

clang++ -std=c++11 -I /usr/local/Cellar/openssl/1.0.2e/include test.cpp -L /usr/local/Cellar/openssl/1.0.2e/lib -lboost_system -lboost_thread-mt -pthread -lcppnetlib-client-connections -lcppnetlib-uri -lcppnetlib-server-parsers -lssl -lcrypto

我大概應該創建一個make文件。

暫無
暫無

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

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