[英]How to use Boost library in QT creator on Ubuntu 14.04
I want to use the C++ Boost library in the QT creator on Ubuntu 14.04, after trying many methods, I still get errors. 我想在Ubuntu 14.04上的QT创建器中使用C ++ Boost库,在尝试了很多方法之后,我仍然会遇到错误。
I installed the Boost library using: 我使用以下方法安装了Boost库:
sudo apt-get install libboost-all-dev
The Boost library is installed in the directory: Boost库安装在以下目录中:
/usr/include/boost
Here is my main.cpp 这是我的main.cpp
#include <QCoreApplication>
#include<iostream>
#include <boost/asio.hpp>
#include <boost/asio/steady_timer.hpp>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
boost::asio::steady_timer timer_;
timer_.expires_from_now(1000);
return a.exec();
}
Any my .pro file: 我的.pro文件:
QT += core
QT -= gui
CONFIG += c++11
TARGET = test_boost_lib_in_QT
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += /usr/include/boost
LIBS += -L/usr/include/boost -lboost_system
LIBS += -L/usr/include/boost -lboost_chrono
LIBS += -L/usr/include/boost -lboost_thread
LIBS += -L/usr/include/boost -lboost_timer
The compiling errors are: (I compiled directly from QT using "ctrl+B") 编译错误是:(我使用“ctrl + B”直接从QT编译)
/home/ndn-experiment/Desktop/test_boost_lib_in_QT/main.cpp:13: error: no matching function for call to 'boost::asio::basic_waitable_timer<std::chrono::_V2::steady_clock>::basic_waitable_timer()'
boost::asio::steady_timer timer_;
^
/home/ndn-experiment/Desktop/test_boost_lib_in_QT/main.cpp:14: error: no matching function for call to 'boost::asio::basic_waitable_timer<std::chrono::_V2::steady_clock>::expires_from_now(int)'
timer_.expires_from_now(1000);
^
What should I do? 我该怎么办?
The first one is because you need to pass io_service
to the constructor: 第一个是因为你需要将
io_service
传递给构造函数:
boost::asio::io_service io;
boost::asio::steady_timer timer(io);
The second one is because expires_from_now
doesn't take int
, it requires a duration
argument: 第二个是因为
expires_from_now
不接受int
,它需要一个duration
参数:
timer.expires_from_now(boost::posix_time::seconds(5));
It's also a good general idea to check the documentation , it has usage examples. 检查文档也是一个很好的总体思路,它有一些用法示例。
After going through so many posts on the Internet and so many errors I encountered, I finally make my program run. 经过互联网上的这么多帖子和我遇到的很多错误后,我终于让我的程序运行了。 Hereby I want to answer my own question, emphasizing two points:
在此,我想回答我自己的问题,强调两点:
another way of creating an object of a class whose constructor takes more than 1 arguments 另一种创建类的对象的方法,其构造函数需要多于1个参数
the right way to use boost::asio::steady_timer
使用
boost::asio::steady_timer
的正确方法
My main.cpp 我的main.cpp
#include <QCoreApplication>
#include <iostream>
#include <boost/asio.hpp>
#include <boost/asio/steady_timer.hpp>
#include <chrono>
#include <boost/chrono.hpp>
using namespace std;
typedef boost::asio::steady_timer timer_type;
class timer_expire{
public:
timer_expire(boost::asio::io_service& io):timer_(io){}
void timer_expires(int n_milliseconds) {
timer_.expires_from_now(boost::chrono::milliseconds(n_milliseconds));
//need timer_.async_wait() here
}
private:
timer_type timer_;
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
boost::asio::io_service io;
timer_expire timer_expire_(io);
timer_expire_.timer_expires(10000);
return a.exec();
}
My .pro file (please note the QMAKE_CXXFLAGS
) 我的.pro文件(请注意
QMAKE_CXXFLAGS
)
QT += core
QT -= gui
CONFIG += c++11
TARGET = test_boost_lib_in_QT
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
CONFIG += c++11
QMAKE_CXXFLAGS += -std=c++11
INCLUDEPATH += /usr/include/boost
QMAKE_CXXFLAGS += -DBOOST_ASIO_DISABLE_STD_CHRONO
LIBS += -L/usr/include/boost -lboost_system -lboost_chrono -lboost_thread -lboost_timer
My g++
version is 4.8.4 我的
g++
版本是4.8.4
Note that there is another way to the boost::asio::steady_timer
by setting the QMAKE_CXXFLAGS
to -DBOOST_ASIO_HAS_STD_CHRONO
, please refer to this post . 请注意,通过将
QMAKE_CXXFLAGS
设置为QMAKE_CXXFLAGS
,还有另一种方法可以使用boost::asio::steady_timer
-DBOOST_ASIO_HAS_STD_CHRONO
,请参阅这篇文章 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.