(转)LINUX 网络编程之Daemon设计

(原)ubuntu 9.10 安装 asio库

孔令春 posted @ 2010年4月01日 07:08 in 网络安全 with tags 安装asio , 4103 阅读

首先将源

deb http://cz.archive.ubuntu.com/ubuntu karmic main 

添加到/etc/apt/sources.list文件中,然后就可以安装了,命令:

sudo apt-get install libasio-dev

卸载:

sudo apt-get remove libasio-dev

 

编译asio手册中的第一个例子 timer.cpp

//
// timer.cpp
// ~~~~~~~~~
//
// Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <iostream> #include <asio.hpp> #include <boost/date_time/posix_time/posix_time.hpp> int main() { asio::io_service io; asio::deadline_timer t(io, boost::posix_time::seconds(5)); t.wait(); std::cout << "Hello, world!\n"; return 0; }

g++ timer.cpp -o timer

结果出错,错误信息:

/tmp/ccRJNtHt.o: In function `asio::detail::posix_thread::~posix_thread()':
timer.cpp:(.text._ZN4asio6detail12posix_threadD1Ev[asio::detail::posix_thread::~posix_thread()]+0x1d): undefined reference to `pthread_detach'
/tmp/ccRJNtHt.o: In function `asio::detail::posix_thread::join()':
timer.cpp:(.text._ZN4asio6detail12posix_thread4joinEv[asio::detail::posix_thread::join()]+0x25): undefined reference to `pthread_join'
/tmp/ccRJNtHt.o: In function `asio::detail::posix_tss_ptr<asio::detail::call_stack<asio::detail::task_io_service<asio::detail::epoll_reactor<false> > >::context>::~posix_tss_ptr()':
timer.cpp:(.text._ZN4asio6detail13posix_tss_ptrINS0_10call_stackINS0_15task_io_serviceINS0_13epoll_reactorILb0EEEEEE7contextEED2Ev[asio::detail::posix_tss_ptr<asio::detail::call_stack<asio::detail::task_io_service<asio::detail::epoll_reactor<false> > >::context>::~posix_tss_ptr()]+0xf): undefined reference to `pthread_key_delete'
/tmp/ccRJNtHt.o: In function `asio::detail::posix_tss_ptr<asio::detail::call_stack<asio::detail::task_io_service<asio::detail::epoll_reactor<false> > >::context>::posix_tss_ptr()':
timer.cpp:(.text._ZN4asio6detail13posix_tss_ptrINS0_10call_stackINS0_15task_io_serviceINS0_13epoll_reactorILb0EEEEEE7contextEEC2Ev[asio::detail::posix_tss_ptr<asio::detail::call_stack<asio::detail::task_io_service<asio::detail::epoll_reactor<false> > >::context>::posix_tss_ptr()]+0x22): undefined reference to `pthread_key_create'
collect2: ld returned 1 exit status
 

看了半天才看明白,原来是缺少多线程,指定参数-lpthread再编译之后成功,命令:

g++ timer.cpp -o timer -lpthread


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter