Compiling TBB programs and examples on Linux Ubuntu Share your comment!


This week I’m going to talk about something I haven’t covered before: Linux. Specially, running Threading Building Blocks on Ubuntu. We’ll look at using TBB with the GNU c++ compiler; soon we’ll also cover topics such as the Cilk Plus extensions for the GNU C++ compiler, as well as the actual Intel Parallel Studio on Linux.

I’m using version 12.04 of Ubuntu here. The steps for installing everything are pretty simple. I’ll assume you already have the GNU C++ compiler. (If you don’t, it’s easy to find instructions on that, elsewhere.)

But you need to install the TBB. The great thing is it’s available through the apt-get command-line package manager. You can also get it through the Software Center, by searching for “tbb” and installing the one called libtbb-dev. In either case, the version I’m seeing right now in the package manager is 4.0, release 223.1. The version on the Threading Building Blocks website is 4.1. But it’s not a big difference.   

To use the command line, just type:

sudo apt-get install libtbb-dev

(I’m assuming you’re logged in as a non-root user who has sudo access.)

Amazingly, this set up everything and I didn’t need to do any additional configuration—no setting environment variables or anything. To try it out, I created a quick C++ file like this, which tests out the parallel_for:


using namespace std;
using namespace tbb;

long len = 5000;
float *set1 = new float[len];
float *set2 = new float[len];
float *set3 = new float[len];

class GrainTest {
void operator()( const blocked_range& r ) const {
//std::cout << r.begin() << std::endl; for (long i=r.begin(); i!=r.end(); ++i ) { set3[i] = (set1[i] * set2[i]) / 2.0 + set2[i]; } } }; int main() { parallel_for(blocked_range(0,len, 100), GrainTest() );
return 0;

You can use your favorite text editor; I’m using scite, which you can install through apt-get with the name scite.

Also, I’m running my Linux in a virtual machine; ideally you wouldn’t want to do this, especially if the virtual machine is limited on what cores it can see. You want to be able to use all the cores! So I lowered the length of my arrays to be really small, primarily to see if it would compile and run.

To compile it, you can execute the g++ command from the command-line like so:

g++ test1.cpp -ltbb

where test1.cpp is what I called the C++ file. I’m not specifying a name for the final executable, so by default it’s a.out. You can change it to something better with the –o option.

There’s also a nice set of examples available. These are the standard TBB examples, and are again available through apt.get. Here’s how you install them:

sudo apt-get install tbb-examples

When you do this, the examples will be put in a strange place, /usr/share/doc/tbb-examples. I recommend moving it to your own home directory (using sudo mv) and then changing both the owner and group to yourself to make it easy to try them. To do that, make sure you’re in the directory containing tbb-examples, and run:

sudo chown -R jeff tbb-examples
sudo chgrp -R jeff tbb-examples

but replacing “jeff” with your own username. 

After that, take a look at the README.Debian file in the root example directory, and follow the instructions for building it, including the line showing how to unzip all the .gz files. All the source and Makefiles start out compressed for some reason. Note, though, that running make doesn’t just compile each example; it actually runs it afterwards. You can go poking through the makefiles if you want to change this behavior. (Hint: the “all” target runs both release and test targets.)

Take a close look at these examples. They’re very good, and they all compile and run beautifully on Linux, and they should give you what you need to get started.

Posted on January 4, 2013 by Jeff Cogswell, Geeknet Contributing Editor