krisdigitx krisdigitx - 6 months ago 30
Linux Question

boost synchronous client error

i am new to boost::asio library, while trying this basic synchronous client, its giving this error

#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>

using namespace std;


using namespace boost::asio;

int main()
{

io_service service;
ip::tcp::endpoint ep( ip::address::from_string("127.0.0.1"), 80);
ip::tcp::socket sock(service);
sock.connect(ep);


}


error while compiling,

[root@kris server]# g++ -o test1 test1.cpp
tmp/ccnLbhhW.o: In function `__static_initialization_and_destruction_0(int, int)':
test1.cpp:(.text+0x11b): undefined reference to `boost::system::get_system_category()'
test1.cpp:(.text+0x127): undefined reference to `boost::system::get_generic_category()'
test1.cpp:(.text+0x133): undefined reference to `boost::system::get_generic_category()'
test1.cpp:(.text+0x13f): undefined reference to `boost::system::get_generic_category()'
test1.cpp:(.text+0x14b): undefined reference to `boost::system::get_system_category()'
/tmp/ccnLbhhW.o: In function `boost::system::error_code::error_code()':
test1.cpp:(.text._ZN5boost6system10error_codeC2Ev[_ZN5boost6system10error_codeC5Ev]+0x17): undefined reference to `boost::system::get_system_category()'
/tmp/ccnLbhhW.o: In function `boost::asio::error::get_system_category()':
test1.cpp:(.text._ZN5boost4asio5error19get_system_categoryEv[boost::asio::error::get_system_category()]+0x5): undefined reference to `boost::system::get_system_category()'
/tmp/ccnLbhhW.o: In function `boost::asio::detail::posix_thread::~posix_thread()':
test1.cpp:(.text._ZN5boost4asio6detail12posix_threadD2Ev[_ZN5boost4asio6detail12posix_threadD5Ev]+0x26): undefined reference to `pthread_detach'
/tmp/ccnLbhhW.o: In function `boost::asio::detail::posix_thread::join()':
test1.cpp:(.text._ZN5boost4asio6detail12posix_thread4joinEv[boost::asio::detail::posix_thread::join()]+0x2b): undefined reference to `pthread_join'
/tmp/ccnLbhhW.o: In function `boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::task_io_service<boost::asio::detail::epoll_reactor<false> > >::context>::~posix_tss_ptr()':
test1.cpp:(.text._ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceINS1_13epoll_reactorILb0EEEEEE7contextEED2Ev[_ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceINS1_13epoll_reactorILb0EEEEEE7contextEED5Ev]+0x15): undefined reference to `pthread_key_delete'
/tmp/ccnLbhhW.o: In function `boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::task_io_service<boost::asio::detail::epoll_reactor<false> > >::context>::posix_tss_ptr()':
test1.cpp:(.text._ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceINS1_13epoll_reactorILb0EEEEEE7contextEEC2Ev[_ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceINS1_13epoll_reactorILb0EEEEEE7contextEEC5Ev]+0x28): undefined reference to `pthread_key_create'
collect2: ld returned 1 exit status


i am using the following libraries on centos 6.3

boost-thread-1.41.0-11.el6_1.2.x86_64
boost-serialization-1.41.0-11.el6_1.2.x86_64
boost-test-1.41.0-11.el6_1.2.x86_64
boost-devel-1.41.0-11.el6_1.2.x86_64
boost-system-1.41.0-11.el6_1.2.x86_64
boost-wave-1.41.0-11.el6_1.2.x86_64
boost-iostreams-1.41.0-11.el6_1.2.x86_64
boost-filesystem-1.41.0-11.el6_1.2.x86_64
boost-python-1.41.0-11.el6_1.2.x86_64
boost-regex-1.41.0-11.el6_1.2.x86_64
boost-program-options-1.41.0-11.el6_1.2.x86_64
boost-date-time-1.41.0-11.el6_1.2.x86_64
boost-signals-1.41.0-11.el6_1.2.x86_64
boost-graph-1.41.0-11.el6_1.2.x86_64
boost-1.41.0-11.el6_1.2.x86_64


any ideas?

update:

running with

[root@kris server]# g++ -o test1 test1.cpp -lboost_system


produces this error:

/tmp/ccwHGnkt.o: In function `boost::asio::detail::posix_thread::~posix_thread()':
test1.cpp:(.text._ZN5boost4asio6detail12posix_threadD2Ev[_ZN5boost4asio6detail12posix_threadD5Ev]+0x26): undefined reference to `pthread_detach'
/tmp/ccwHGnkt.o: In function `boost::asio::detail::posix_thread::join()':
test1.cpp:(.text._ZN5boost4asio6detail12posix_thread4joinEv[boost::asio::detail::posix_thread::join()]+0x2b): undefined reference to `pthread_join'
/tmp/ccwHGnkt.o: In function `boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::task_io_service<boost::asio::detail::epoll_reactor<false> > >::context>::~posix_tss_ptr()':
test1.cpp:(.text._ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceINS1_13epoll_reactorILb0EEEEEE7contextEED2Ev[_ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceINS1_13epoll_reactorILb0EEEEEE7contextEED5Ev]+0x15): undefined reference to `pthread_key_delete'
/tmp/ccwHGnkt.o: In function `boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::task_io_service<boost::asio::detail::epoll_reactor<false> > >::context>::posix_tss_ptr()':
test1.cpp:(.text._ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceINS1_13epoll_reactorILb0EEEEEE7contextEEC2Ev[_ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceINS1_13epoll_reactorILb0EEEEEE7contextEEC5Ev]+0x28): undefined reference to `pthread_key_create'
collect2: ld returned 1 exit status

Answer

You have to add -lboost_system and -lpthread to your g++ command line, like this:

g++ -o test1 test1.cpp -lboost_system -lpthread