Wednesday, August 15, 2012

Boost Threading XCode + Boost Library Example

To do threading in boost you need to have compiled the Boost Libraries ./b2
The libraries are stored in /[boost install path]/stage/lib folder

Make sure you set the "Header Search Path" in XCode to /usr/local/include then add all the "dylib" files to your project.

.dylib = dynamic library files
.a = static library files

Here's a really good reference for using dynamic libraries with xcode Dynamic Library Reference

Navigate to the project Settings click on target and where it says "Link Binaries with Libraries" add your library files there, be sure to add all of them.

  

Now comes time for the coding.

In main.cpp add the following code, you should be able to run it.
 #include <iostream>  
 #include <boost/thread.hpp>  
 using namespace boost;  
 using namespace boost::this_thread;  
 using namespace std;  
 // Global function called by thread  
 void GlobalFunction()  
 {  
   for (int i=0;i<10;++i)  
   {  
     cout << i << "Do something in parallel with main method." << endl;  
     boost::this_thread::yield();  
   }  
 }  
 void GlobalThreadTest()  
 {  
   boost::thread t(&GlobalFunction);  
   for (int i = 0; i<10; ++i) {  
     cout << i << "Do something in main method. " << endl;  
   }  
 }  
 int main(int argc, const char * argv[])  
 {  
   GlobalThreadTest();  
   return 0;  
 }  







Using Boost with XCode First Project

This Project involves setting up XCode to use boost. It's very simple, it does not involve using compiled libraries.

Assuming that you have followed the steps on the previous post on Installing Boost on OSX and have copied the libraries /usr/local/include/boost

Note: If you have used MacPorts your libraries are stored in /opt/local/include/boost

1. Open XCode and create a new C++ Project
2. Click on the project to open the settings and search for a parameter called "Header Search Paths"
change it to /usr/local/include. Boost libraries should be copied here.

Boost XCode 4.4 Setup
That's really it. You don't have to add anything to the target or give XCode a library search path. Make sure "Always Search User Paths" is set to "No" and you are good.

Boost First Program 
 #include <boost/array.hpp>  
 #include <iostream>  
 int main()  
 {  
   boost::array<int, 10>myarray;   
   myarray.fill(2);  
   for (int i=0; i<10; i++) {  
     std::cout << myarray.at(i) << std::endl;  
   }  
   return 0;  
 }  

First Boost Program Download


 

Installing Boost 1.50+ on OSX

There have been many posts about how to install Boost on OSX most of these deal with older versions or recommend using MacPorts. The difference between this posting is that we are not going to be using MacPorts. MacPorts just copies the headers, it doesn't really build the libraries or if it does I don't know where it stores them.

Things to know:
  1. Boost install comes with the headers, no compilation is needed.
  2. The only things you need to compile are boost thread, signals, things like that.
  3. The only Boost libraries that must be built separately are:
    1. Boost.Filesystem
    2. Boost.GraphParallel
    3. Boost.IOStreams
    4. Boost.MPI
    5. Boost.ProgramOptions
    6. Boost.Python (see the Boost.Python build documentation before building and installing it)
    7. Boost.Regex
    8. Boost.Serialization
    9. Boost.Signals
    10. Boost.System
    11. Boost.Thread
    12. Boost.Wave
Building the boost libraries is pretty easy and straight forward.

Preliminary Steps (Things you need)
- XCode 4.4 or Latest
- XCode Command Line Tools Installed
-- To check do a [Command+, ] and go to the downloads tab.


First steps to installing boost.
  1. Download latest version of the boost zip file and extract it
  2. Extract the file 


boost install path = where you extracted the zip file to.

/[boost install Path]/boost Contains all the headers. Use Termianl to copy the header files to
/usr/local/include/boost [XCode header search path] You will later use this path to set up your boost project in XCode.
sudo mv /[boost install path]/boost /usr/local/include/boost
    3.  Compile the Boost Libraries! Most of the boost scripts are set to read+write only so if you try and compile them by running sudo ./bootstrap.sh you'll see the following error messages,
sudo: ./boostrap.sh: command not found
To overcome this you have to do run the command: chmod u+x *.sh do this in the boost folder and also in [boost install path]/tools/build/v2/engine/

Now the you can compile boostrap.sh. This will build b2. Running ./b2 will compile all the libraries such as threading.

    4. After boostrap has been executed you should see b2. Do a sudo ./b2 and this will create the libraries. Boost libraries will be created in the [boost install path]/stage/lib

Now you are ready for the next step! Adding boost to XCode.