问题描述:

I want to use the C++ Boost library in the QT creator on Ubuntu 14.04, after trying many methods, I still get errors.

I installed the Boost library using:

sudo apt-get install libboost-all-dev

The Boost library is installed in the directory:

/usr/include/boost

Here is my 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:

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")

/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:

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:

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:

  1. another way of creating an object of a class whose constructor takes more than 1 arguments

  2. the right way to use boost::asio::steady_timer

My 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)

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

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.

Also take a look at Chrono.

相关阅读:
Top