This tutorial describes Xcode projects are set up to incoroporate the GNU Scientific Library.

GSL installation

Users of MacPorts or the Fink Project may want to install the GSL via this route. Installation from source files is described briefly.

To ascertain whether the library is already installed on the system, execute the following command in a terminal window.

 $ gsl-config --cflags --libs-without-cblas
   -I/opt/local/include
   -L/opt/local/lib -lgsl -lm

The information provided by the --cflags and --libs-without-cblas flags is essential for compiling and linking programs to use the library. This example output indicates that all the GSL's header files are in the path /opt/local/include, which for instance will be the case when the GSL was installed using MacPorts. An installation from Fink will have different paths.

Please skip to GSL with Xcode projects if the library is installed.

Installation from source files

By its default configuration, the GSL will be installed in the path /usr/local. Unfortunately this path is invisible to the Finder on OS X systems. (However the library is still usable in Xcode projects if it happens to be installed in this path!)

For instructive purposes this example shows how to install the GSL in the path Developer/usr.

  1. Download and unpack the current version of the GSL.
  2. Follow the standard steps for UNIX systems:
     $ ./configure --prefix=/Developer/usr
     $ make
     $ sudo make install
    

The --prefix flag for the configure script sets the root for the header paths and the librarie paths. The command ./configure --help shows more configuration options.

GSL with Xcode projects

Xcode can manage a project that uses an "external build system" such as GNU make to control the compiling/linking dependencies. More importantly it is also a full IDE with its own drag-and-drop style manager for build control.

This tutorial builds a small test utility for GSL random number generators.

/* test_rng.c */

#include <stdio.h>
#include <gsl/gsl_rng.h>

gsl_rng * r;  /* global generator */

int
main (void)
{
   const gsl_rng_type * T;
   
   gsl_rng_env_setup();
   
   T = gsl_rng_default;
   r = gsl_rng_alloc (T);
   
   printf ("generator type: %s\n", gsl_rng_name (r));
   printf ("seed = %lu\n", gsl_rng_default_seed);
   printf ("first value = %lu\n", gsl_rng_get (r));
   
   gsl_rng_free (r);
   return 0;
}

The source code is also available at test_rng.c.

First create an "empty project" with Xcode. This is to take advantage of Xcode's internal build system. In this tutorial, the project is called example.

new_project.png

The next step is to add a new "shell tool" to the project. This is under the menu Project > New Target. Here the utility will be called test.

new_target.png

The next several steps set important global parameters of the project.

Machine architecture

Open the Info window by selecting the root folder in the project window and clicking the blue Info button. In the Valid Architectures field, simply remove all architectures that do not apply.

valid_arch.png

On a new Intel machine, the x86_64 architecture suffices. Trying to link an object that is compiled for both a Power PC and an Intel machine, for instance, will lead to a conflict with the GSL's dynamic libraries.

Simplifying the build directory

Xcode has two default build configurations, Debug and Relase, and it places respective products in different paths for organization. This tutorial uses only the Debug configuration. For simplicity all products will be placed in the project's root directory.

First, delete the "/build" segment of the Build Products path.

custom_location.png

Next, under the Build tab in the same window, modify the Per-configuration Build Products Path by removing the CONFIGURATION and EFFECTIVE_PLATFORM_NAME variables. Ensure that the path name does not end with a forward-slash.

per-config_build_prod_path.png

Compiler and linker flags

Xcode needs to know where the header files and library files for the GSL are located on the system. As outlined in GSL installation, this information obtained by passing the --cflags and --libs-without-cblas flags to the gsl-config utility.

  1. Locate the Build > Header Search Paths field in the Project Info window. Add the path returned by the gsl-config --cflags command, excluding the leading "-I". Do not check the "recursive" box.
  2. Next locate the Library Search Paths field. Add the path returned by the gsl-config --libs-without-cblas command, excluding the leading "-L". Again, do not check the "recursive" box.
  3. Locate the Other Linker Flags field. Add the remaining flags returned by --libs-without-cblas verbatim.
set_header_search_path.png

Adding source files

The GSL is now ready for use with Xcode. To see that it works:

  1. Save the source file test_rng.c and add it to the project via Project > Add to Project. Xcode uses absolute paths, so the file does not have to be in the home directory.
  2. Add the source file to the Compile Sources folder of the shell utility.
  3. Build the target with the ⌘B command.
added_to_target.png

That's all!