How to use Boost library in QT creator on Ubuntu 14.04
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
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.
linux : make boost work with Qt Creator (undefined reference)
Thanks @cv_and_me for the answer :
The error was that I was not including the good library.
LIBS += \
-lboost_system
should be replaced by
LIBS += \
-lboost_unit_test_framework
I made the error to belive that boost was only for unit testing, but it could do a lot more, so we have to add the right part of boost that we will be using (in my case, the unit testing framework).
(for people at the same stage than this post, I then used this tutorial for making a good project structure, separating unit tests and the app : http://dragly.org/2014/03/13/new-project-structure-for-projects-in-qt-creator-with-unit-tests/)
Qt project with Boost
I believe I have found the solution! I noticed that I had two copies of every boost library in \stage\lib.
libboost_system-mgw53-mt-1_65_1.a
and libboost_system-mgw53-mt-d-1_65_1.a
I compiled the project using both and while the former had some warnings, the latter did not. Removing libboost_system-mgw53-mt-1_65_1.a
from the folder allowed -lboost_system-mt
to work just fine.
I don't know why I had both of these files, but it was undoubtedly due to me fumbling around with compiling boost.
How to configure Qt Creator to use Boost in Windows
I did solve the problem myself. And here is how I did it. First of all, it is required to have boost library compiled with same compiler you're using with Qt.
If you're using msvc, then you're lucky because Boost guys did you a favour and compiled libraries for you. You can download them here: http://sourceforge.net/projects/boost/files/boost-binaries/ .
If you're using mingw (which does come in bundle with Qt), you can do this:
- add mingw compiler to Windows PATH variable:
~ go to control panel and search for System;
~ add mingw's path(e.g. C:\Qt\Tools\mingw\bin) to PATH variable by appending ';' to your path(e.g.: ";C:\Qt\Tools\mingw\bin") - compile Boost libraries:
~ unzip boost archive
~ open a Command Line window, go in the unzipped boost folder, then go in folder tools/build/v2/engine
~ you have you build installer with mingw toolset: .\build --toolset=mingw
~ this will create 2 files in folder bin.ntx86 or something similar; copy the files bjam and b2 in the unzipped boost folder;
~ now go in boost folder and start build it: .\b2 --toolset=mingw --build-type=complete stage (there is good tutorial to install it along with eclipse : http://theseekersquill.wordpress.com/2010/08/24/howto-boost-mingw/)
note: this gonna take few hours, so may want to watch a movie or what ever you want to do meanwhile. However you have the option to speed up things a little bit by adding another argument to the build command: -j N, where N is how many cores your processor have. - when build has finished, you can now link the library in Qt. To do this you need to modify .pro file. First you'll have to tell Qt where are headers are located, and you do so by adding:
INCLUDEPATH += path_to_boost_folder, e.g. : INCLUDEPATH += C:/boost_1_54_0
~ also if you're using libraries which requires link, for example system and filesystem you have to link them separately:
LIBS += "C:/boost_1_54_0/stage/lib/libboost_filesystem-mgw48-1_54.a",
LIBS += "C:/boost_1_54_0/stage/lib/libboost_system-mgw48-1_54.a" - after modifying the .pro file, run qmake, then rebuild.
Hope this works for you too!
Update: The folder hierarchy has change. For building the library, one should read the documentation associated with each version and Boost.Build's documentation.
Building the library from the root folder is easier (Building Boost 1.52 with MinGW):
C:\boost_1_60_0> bootstrap.bat mingw
C:\boost_1_60_0> .\b2 --toolset=gcc -j N --build-type=complete
Related Topics
Best Way Elevate the Privileges Programmatically Under Different Versions of Linux
How to Use Stdin Twice from Pipe
Crontab Is Not Working on Amazon Ec2 Server
Recursively Find Files with a Specific Extension
What Does Anon-Rss and Total-Vm Mean
Using Bash Script to Find Line Number of String in File
Shell-Init: Error Retrieving Current Directory: Getcwd -- the Usual Fixes Do Not Wor
If I Have a Process, and I Clone It, Is the Pid the Same
How to Use Global Arrays in Bash
Is Kernel Space Mapped into User Space on Linux X86
Why Does Perf Show That Sleep Takes All Cores
How to Set Up Curl to Permanently Use a Proxy
Fork: Retry: Resource Temporarily Unavailable