Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile c++ code with boost lib? on Ubuntu

Tags:

c++

ubuntu

boost

#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

I used headers shown above. Cannot find help on Boost Official website.

like image 270
Don Lun Avatar asked May 05 '11 00:05

Don Lun


People also ask

How do you compile with Boost?

To compile anything in Boost, you need a directory containing the boost/ subdirectory in your #include path. depending on your preference regarding the use of angle bracket includes. Don't be distracted by the doc/ subdirectory; it only contains a subset of the Boost documentation. Start with libs/index.

Does Boost work with C?

Boost can't be used with C as it uses OOP features from C++. It may be technically possible to develop a wrapper for it.


1 Answers

Assuming no errors in your code, you need to link the correct boost libraries: boost_thread, boost_system, and boost_date_time are the ones you've referenced, so

#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
int main()
{
}

This program compiles with the following command:

g++ -o test test.cc -lboost_system -lboost_date_time -lboost_thread
like image 84
Cubbi Avatar answered Sep 23 '22 06:09

Cubbi