4.2. Tougher Test with Threads

The directory ${base}/libs/thread/tutorial contains a number of sample programs to test out with Boost threads. I copied these to some random place under my ${HOME} directory and needed the following Makefile:

.PHONY: all clean
ALL = helloworld helloworld2 helloworld3 factorial factorial2
LIBS=-lboost_thread_dynamic -lpthread
.cpp:
    ${CXX} -o $@ $^ ${LIBS}
all: ${ALL}
clean:
    ${RM} core* *.o
    ${RM} ${ALL}
   

Notice how you have to supply the platform-dependent -lpthread in order to get it all to work. Another way to build these tutorials is to go to the directory ${base}/libs/thread/tutorial and type bjam (and nothing more). As an example, for hellowrld this will create a labrynth of directories starting from where you typed bjam from: bin/helloworld/gcc/release/runtime-link-static/threading-multi. At the end, you'll find a completely, 100% statically linked executable (not even the linux or c++ libraries are dynamic). The files we created above (with the Makefile) were dynamically linked.

Also notice the order of the way the libraries are included. I had to create a suffix rule so that the libraries would end up at the end of the compile line, instead of at the front or in the middle. This has to do with symbol resolution of the linker (ld). Due to the way that the Boost library was created, it must appear at the end of the compile line, because of the way dependencies are handled.