我正在尝试使用Boost的Asio构建一个项目,我遇到了一些麻烦.最初,我试图在没有任何额外库的情况下构建项目,因为所有内容都应该在头文件中.
我正在尝试构建的程序如下所示:
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
int main()
{
boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
t.wait();
std::cout << "Hello, world!" << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
可以发现这里在加速的网站.
所以,最初我只是:
-I /usr/include/boost_1_40_0
Run Code Online (Sandbox Code Playgroud)
这导致以下错误:
make -k all
Building target: HelloWorld
Invoking: GCC C++ Linker
g++ -o"HelloWorld" ./main.o
./main.o: In function `__static_initialization_and_destruction_0':
/usr/include/boost_1_40_0/boost/system/error_code.hpp:205: undefined reference to `boost::system::get_system_category()'
/usr/include/boost_1_40_0/boost/system/error_code.hpp:206: undefined reference to `boost::system::get_generic_category()'
/usr/include/boost_1_40_0/boost/system/error_code.hpp:211: undefined reference to `boost::system::get_generic_category()'
/usr/include/boost_1_40_0/boost/system/error_code.hpp:212: undefined reference to `boost::system::get_generic_category()'
/usr/include/boost_1_40_0/boost/system/error_code.hpp:213: undefined reference to `boost::system::get_system_category()'
./main.o: In function `boost::asio::error::get_system_category()':
/usr/include/boost_1_40_0/boost/asio/error.hpp:218: …Run Code Online (Sandbox Code Playgroud) 我尝试在这个简单的测试应用程序中使用boost deadline_timer,但遇到了一些麻烦.目标是定时器使用expires_at()成员函数每45毫秒触发一次deadline_timer.(我需要一个绝对的时间,所以我不考虑expires_from_now().我现在也不关心漂移).当我运行程序时,wait()不等待45毫秒!然而,没有报告错误.我是否以某种方式错误地使用了库?
示例程序:
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
using namespace std;
int main()
{
boost::asio::io_service Service;
boost::shared_ptr<boost::thread> Thread;
boost::asio::io_service::work RunForever(Service);
Thread = boost::shared_ptr<boost::thread>(new boost::thread(boost::bind(&boost::asio::io_service::run, &Service)));
boost::shared_ptr<boost::asio::deadline_timer> Timer(new boost::asio::deadline_timer(Service));
while(1)
{
boost::posix_time::time_duration Duration;
Duration = boost::posix_time::microseconds(45000);
boost::posix_time::ptime Start = boost::posix_time::microsec_clock::local_time();
boost::posix_time::ptime Deadline = Start + Duration;
boost::system::error_code Error;
size_t Result = Timer->expires_at(Deadline, Error);
cout << Result << ' ' << Error << ' ';
Timer->wait(Error);
cout << Error …Run Code Online (Sandbox Code Playgroud)