我最近开始学习cpp-netlib,它需要Boost并且在尝试从网站http://cpp-netlib.org/0.11.2/examples/http/http_client.html编译其中一个cpp-netlib示例时遇到了一些问题。我之前从未使用过Boost,并且想知道在编译程序时我应该如何确定要链接的内容?我目前正在使用自制软件作为我的包管理器。我通过运行
安装了boostbrew install boost --c++11
然后我通过运行
安装了cpp-netlibbrew 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 ++发现我的Mac已经过时的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以&#34;总线错误退出:10&#34;。 我发现运行这行代码时出现了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
非常感谢任何帮助!
答案 0 :(得分:1)
我发现要摆脱分段错误,我需要包含-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文件。