© T. Kam | You are here: » timothy.kam » play » computing » gsl_xcode »

C Programming: Using GSL in Apple Xcode

Step 0: Checking existence of GSL (and Fink)

I use Fink to install the GNU Scientific Library (GSL). Fink is like Aptitute (high-level package manager) in Linux. If you are not sure if GSL is already installed, at the Terminal prompt $ type:
                     
                            $   gsl-config --cflags --libs-without-cblas

                     
If GSL is installed, the command above should return the following information:

                    -I/sw/include
                    -L/sw/lib -lgsl -lm

                    
This information is vital for compiling and linking C programs that use the GSL. The example output above tells me that all of the GSL header files (files with extension .h) are located in the directory path /sw/include. If GSL is not installed, and if you have Fink, at the Terminal prompt $ issue the command
                        $   sudo fink install gsl
                    
Note there is an option to install Fink and build packages as 64-bit software. But this limits the usability for most other packages that are written for 32-bit machines. If you do not have GSL installed then you first need to have a working installation of Fink. At the time of writing, a binary installer for Fink is not available for MacOS 10.6 Snow Leopard. You can easily compile and build from source using the instructions and Shell script from here.

Step 1: Using Xcode IDE

Xcode is an Integrated Developer Environment (IDE) kind of like Microsoft's Visual Studio (?). I currently use Xcode 3.2.5. It should come with your Mac install DVDs. If not go online to Apple Developer's website, register for free, and download Xcode and the Developer Kit (which includes compilers like GCC and other stuff). If all these are installed, then go to /Developer/Applications/Xcode.app to launch Xcode.

Step 2: Start a new C project

Now it's time to start a new project. Go to File > New Project ... > and then choose under "Mac OSX", Application > Command Line Tool. Name your project. In this example, I called it "my_project_001".

Step 3: Specify Header, Library Paths and Linking Flags

Next, click on the blue Info button on the toolbar. In this example, you'd see on the right, a new panel called: Project "my_project_001" Info. Do the following:
  • Type "Header Search Paths" in the search window. Add the path determined in Step 0 above. Here it is "/sw/include".
  • Type "Library Search Paths" in the search window. Add the path determined in Step 0 above. Here it is "/sw/lib".
  • Type "Other Linker Flags" in the search window. Add the flags which would be used when linking: "-lgsl -lgslcblas".
Now Xcode should be able to find and link to GSL when compiling and linking C code which call the GSL. The figures below summarize 3 steps above. Example: Specifying "Header Search Paths" : /sw/include
Example: Specifying "Library Search Paths" : /sw/lib
Example: Specifying "Other Linker Flags"s : -lgsl -lgslcblas

Step 4: Start Coding, Compiling and Linking, and Debugging ...

By default Xcode will create a file called main.c which must always be there in any C program. Open this file up and start editing it. Then you must compile and link your C code. In Xcode, there is a button called Build and Run that does just this, and it also reads in the options (linker flags) you specified earlier. in this case, these are dynamic links to your GSL libraries: -lgsl -lgslcblas.

Example: The example below calls a GSL function and also performs a FOR loop and displaying a string and numerical output as it goes.