Archive for August, 2009

Compiling Boost on Mac OS X

Posted in Programming on August 20th, 2009 by Simon Connah – Be the first to comment

I often see people asking how to compile the Boost C++ libraries on Mac OS X so I thought I would post a quick tutorial.

  1. Download B-jam from http://www.boost.org/ – you will find it in the “Downloads” area of the site. Make sure you download the Mac OS X binary file to avoid problems.
  2. When B-jam has been download, extract the archive by double clicking on it.
  3. Move the executable entitled “bjam” to a directory on your path. If you don’t know what this means then just open the Terminal application and type “cd ” (the trailing space is very important) then drag the folder which you just extracted onto the Terminal window, click once in the Terminal window and then press return.
  4. Type “sudo mv bjam /usr/bin” exactly as shown, you will then be asked to type your administrator password. Once you have typed it in press return and bjam will now be accessible from the command line.
  5. Next download the source code distribution of Boost from http://www.boost.org/ – this is found in the same place as B-jam.
  6. Extract the archive and then open the folder in Terminal using the same method outlined above.
  7. Type “bjam stage –build-type=complete” and press return. Wait for a couple of hours for it to finish and then type “sudo make install” to install the boost libraries on your machine.

Update: For those who want to build just the 64 bit versions of the shared library that are thread safe in release mode (this is a much, much faster build time) then you can substitute the build command in stage 7 above with this one:

bjam toolset=darwin link=shared threading=multi runtime-link=shared variant=release address-model=64 stage

Update 2: To build a 32 bit version for PowerPC Macs and a 32 bit and 64 bit version for Intel Macs in a universal binary you need to substitute phase 7 listed above for the following command:

bjam toolset=darwin threading=multi variant=release link=shared runtime-link=shared address-model=32_64 architecture=combined

Using Open Source Libraries on Mac OS X

Posted in Programming on August 20th, 2009 by Simon Connah – Be the first to comment

Mac OS X has a rich development environment that is at least the equal of Linux in terms of diversity and available features. For a start Apple supply Xcode, which in my opinion, is one of the best IDEs available on any platform. Included with the Xcode IDE is a large set of tools including GCC, NASM, Flex, Bison, Make and many other tools traditional UNIX programmers will find familiar.

I want to show the more traditional Mac users who perhaps only know about Objective-C and Cocoa what the possibilities are when it comes to using the vast array of open source software on Mac OS X.

The first thing that should be said is that open source libraries normally come in source code form and thus require you to compile them yourself. This is actually a very easy process, and normally just requires you to issue the following commands:

./configure
 
make
 
sudo make install

from the Terminal application. Obviously this will only compile and install the default package, if you want more control over what you want installed or where you want it to be installed then you will need to supply some options to the configure script.

In order to find out what options are available to you just type the following command:

./configure --help

and it should list all the available options that you can pass to the configure script. The default install location for most libraries will be /usr/local which in 99% of cases is fine.

Assuming there are no missing dependencies for the particular library you are trying to compile the configure script should run successfully. Once that is done you can then compile the library using the make command. Once it has compiled all you need to do is install it using the sudo make install command.

Now that you have the library installed you will most likely want to make use of it from within your Xcode projects. This is an area that many people seem to find somewhat confusing so I will try to keep this as simple as possible.

There are two stages to using a library (other than the ones that come with Xcode / the operating system itself). First you must tell Xcode where to locate header files and secondly you must tell Xcode where to find the compiled dynamic or static libraries. The first stage is very simple indeed, with your project open go to the Project menu and select “Edit Project Settings”. This will bring up the build configuration window where you can set numerous options to do with compilation and deployment relating to your project.

Look for an item called “Header Search Paths” in the Search Paths section shown in the image below.

Xcode Build Settings

Xcode Build Settings

My project as you can see already has the header and library search path defined. In this case it tells the compiler to look in /usr/local/pgsql/include for header files and /usr/local/pgsql/lib for library files. If you see a star after the path it means that you have told Xcode to search recursively all sub folders of the specified path for the header and library files that you have included. Be careful with this option as it can lead to some odd build errors but that has only happened to me once so your milage may vary.

When passing the default values to a configure script it almost always installs the files in /usr/local. Given this assumption you should put /usr/local/include in the Header Search Paths box and /usr/local/lib in the Library Search Paths box. Obviously if you told the configure script to install it somewhere else then adjust the paths accordingly.

The last step is to add the library itself to the Xcode project. With your project open go to the Project menu again and select the option “Edit Active Target” (it will have the name of your application after that). This will open the following window:

Xcode Library Settings

Xcode Library Settings

As you can see the bottom panel already includes two libraries in my project and both of them are required dependencies. You can add your own library dependencies by just clicking the bottom “+” button and selecting the required library / framework from the list that pops up (assuming you set the paths correctly in the preceding set of instructions).

That is pretty much all there is too it. Hopefully that will have opened up a whole wealth of open source software which you can now make use of in your own projects (make sure you comply with their licenses though :) ); everything from audio, graphics, maths, science and language is covered in one form or another by open source software so I am sure that there will always be something that you find useful after a little searching.

As always, if you have any questions or comments please post below and I will try to get back to as soon as possible.

Compiling and Installing NASM on Mac OS X

Posted in Programming on August 19th, 2009 by Simon Connah – 6 Comments

First things first, make sure you have installed the latest version of Xcode from the Apple Developer website (the version that comes on the Mac OS X DVDs is most likely an older version). Once that is installed you need to download the latest source code distribution of NASM from here, it is located under the Download tab.

Once you have the file downloaded (it should be called something like nasm-x-xx.tar.bz2 – if it has RC in the name download the next lowest number that does not have RC in the file name as we want to use a stable version for the purposes of learning) just double click on it to extract its contents into a folder.

Now open up the Terminal application which is located in /Applications/Utilities/ and type “cd” followed by a space. Then drag the folder that was just extracted from the nasm archive and drop it onto the Terminal window. This should place the path to the folder after the cd and space. If it looks something like:

cd ~/Downloads/nasm-2.07

then you have done it correctly. Press return and the terminal should place you inside the nasm distribution folder. Type the following commands exactly as they appear below:

./configure
 
make
 
sudo make install

after you have typed sudo make install it will ask you for your password. This is your administrator account password. It will not show anything as you type as a security measure so make sure you don’t forget how far you have got :) .

Now inside the Terminal window if you type

nasm -v

you should see the following (or something similar) output:

NASM version 2.07 compiled on Aug 13 2009

congratulations, you now have the latest version of NASM installed. If you have any questions please leave a comment and I will try my best to answer them (perhaps in a new blog post).