boyfarrell.com

4 April 2008

GNU Scientific Library and XCode 3.1

Assuming you have a working installation of GNU Scientific Library either from source, Fink or MacPorts, this tutorial will show you how to getting a working XCode project. I am currently using XCode 3.1 but this process hasn’t changed much over the last few years, so will work on older systems.

Download a GSL Example project ready for your own code:
XCode 3.0+ GSL Example Project
XCode 2.4+ GSL Example Project

  1. Create a new project, I called mine, ‘GSL Project’.
    Create project

  2. Choose a default template, New Cocoa application will do nicely.
    Template

  3. Select get info on the project icon. As we need to tell xcode where GSL is.
    Get info

  4. Select the native option in the architectures list. Xcode can build dual binaries (DB), however, GSL is only compiled for the native machine, so it it tried to make a DB is will FAIL!
    native

  5. Type header search paths into the search box. Editing the ‘Header Search Paths’ field, add the following paths.

    hsp
    hsp fields

  6. Next we will do the same process as but for the ‘Library search paths’,

    lsplsp fields

  7. Now search for ‘other linker flags’ and set the following values,
    olf
    olf field

  8. We are done, but let’s do a test program. Select the main.m file in the File & Groups list. You may need to press the ‘Editor’ button in the button menu to see the contents of the file,
    main.m cocoa

  9. Select all and replace with some GSL test code, build and run.
    main.m gsl

Make sure that you have the DEBUG build settings active. Have your project in RELEASE mode won’t work (thanks for point that out Paul).

21 Comments currently posted.

Guillaume says:

Hi,

Thanks for the nice tutorial! However, I have not been able to have it worked with Xcode 3.0. I have GSL correctly installed on my MacPro, as I can use it if compiling in the terminal. But in Xcode, I get this:

ld: warning in /Developer/SDKs/MacOSX10.5.sdk/usr/local/lib/libgsl.a, file is not of required architecture
ld: warning in /Developer/SDKs/MacOSX10.5.sdk/usr/local/lib/libgslcblas.a, file is not of required architecture
“_gsl_vector_alloc”, referenced from:
_main in main.o
“_gsl_vector_set”, referenced from:
_main in main.o
“_gsl_vector_get”, referenced from:
_main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

There is a slight difference between Xcode 3.0 and 3.1, and I don’t have the same window to select the architecture. But the settings I managed to have are:

ARCHS = x86_64
VALID_ARCHS = x86_64

But it still does not work. Do you have any idea?

Thanks !!

boyfarrell says:

Hi,

Hmmm…. You could try deleting the VALID_ARCHS and ARCHS settings. When you specify nothing I think it defaults to native, but I could be wrong.

I have an iMac so I will post an Empty GSL project for xcode 3.0 for you.

Cheers,

Dan

boyfarrell says:

Hello again!

I have just had a mess around and it doesn’t default to native by deleting those flags,

try setting them to,

ARCHS = i386
VALID_ARCHS = i386

This worked with my new iMac.

Cheers,

Dan.

Guillaume says:

True! This works! Thanks so much! But I think I should elucidate how to compile the GSL to allow x86_64. I suppose computations will go faster then?

boyfarrell says:

Hi,

You could try making this change, http://lists.apple.com/archives/scitech/2008/Mar/msg00012.html during compilation.

Dan

Paul says:

Well, I followed your tutorial (after I figured out I had to dl the iPhone SDK to get XCode 3.1), but when I click on ‘build and go’, using the sample code you linked to, I get build errors. It says “error: gsl/gsl_vector.h: No such file or directory”. I double checked that I set the search paths correctly, and that my gsl was actually located in those paths, and everything was good, and I even tried replacing the

#include

with

#include “gsl/gsl_vector.h”

but to no avail. Any idea what I’m forgetting? I notice that in the project window, there is no mention of gsl anywhere, even under the frameworks area. Is that significant?

Paul says:

Ah, disregard. I had it in release mode, and only set the paths for debug mode.

boyfarrell says:

Ah! That one still gets me :o) I have made a note in the tutorial to remind people. Thanks for your comment. But just to check other than that it worked?

Dan.

Paul says:

Yes and no. Although I had to muck a bit with XCode to figure out how to get it to actually pop up the output console when I run the program, it does run with the Build and Go button, or by selecting Go or Run from the Run menu. However, when I try to execute the application itself from finder (by digging into the Build>Debug folder [I compiled under Debug mode]), the application bombs, and I get a big nasty error report. I’m not sure if this is due to the code snipit I used (it has an intentional error in it) or something else… I’ll try modifying it to remove the error and rebuild.

Paul says:

Well, fixing the error in the code resolved the crash problem (duh), but I still can’t get the program to open a terminal window when it executes, and I can’t figure out how to run it from terminal (./ doesn’t work… says that the .app is a directory, and I couldn’t find a proper executable when I dove into the package). I even tried adding the lines:

char a;
scanf(”%c”, &a);

at the end of the program to keep the terminal window from closing, but it never even pops one up. Any clue how to fix this?

Paul says:

Ok, got ./ to work.. APPARENTLY you can’t have a space between the ./ and the executable name…. But still can’t get the compiled program to execute from finder (unless I dive into the package, of course, but who wants that?)

I can’t help but think that there must be an easier way to do all this, without having to resort to entering the search paths every time I make a new project, or mucking with the linker flags etc. Is there some way to compile GSL into an XCode framework so that it can then be easily added to the project via intuitive methods? Like the Cocoa framework?

Paul says:

BTW, it would be nice if you included some instructions up at the top on how to install GSL. I finally got it installed, but it took me forever to figure out how to do it. I tried using MacPorts at first, but couldn’t get that to work, so I eventually just compiled it from the source. These are the steps I used:

1. Download and decompress the latest build of GSL. Note that it’s a tar.gz, so you MAY have to decompress it twice, though Mac OS X should take care of it for you. I used 1.11, and got it from the GNU FTP server here: ftp://ftp.gnu.org/gnu/gsl/

2. Drag the folder (which you just expanded) containing GSL to the Desktop.

3. Go into your Utilities folder in the Applications folder, and open the program called, “Terminal”.

4. Check to make sure the title of the window says ‘bash’ somewhere. When the command line pops up, enter the following:

cd ~
cd Desktop
cd gsl*
./configure
make
sudo make install

boyfarrell says:

Hi Paul,

You wrote the post for me! Thanks for the tutorial, I will add some screen-shots and post it up.

Your idea for a framework of GSL is a good. This way it would be easier to deploy on different system i.e it could be compiled as a dual-binary and all you would need would be the one framework file…. i’ll look into it. There might be some licensing issues…

That said, I have always found that the GSL people do an EXCELLENT job of packaging their code, I have never had it break. Just like you said all you need is:

./configure
make
sudo make install

Regarding the other points you raised, sound like what you are looking for is the Xcode Console (APPLE-SHIFT-R), from the Run menu. And if you want to execute the program from the terminal, you need to dig down to the actual unix executable. It is inside the .app folder e.g. cd demo.app/Contents/MacOS/ then ./demo will run the executable (assuming the .app is called demo).

Bill says:

Thank you so much for this valuable information. It made my day. I was having a lot of frustration with the architecture issue before reading this.

boyfarrell says:

Hi Bill,

Did you use one of the stand-alone project to get it working or did you notice that in the comments somebody got it to work by selecting the i386 arch? Maybe I should add something about the ARCH flags to the post? It seems something that people are having problems with?

What mac are you using and which version of xcode?

theCodeMaiden says:

Thank you so much! I couldn’t figure out how to statically link GSL for the life of me… and you’ve made it so simple!

Anonymous says:

…i am using leopard and xcode 2.5
i followed your tutorial….but i got the following…
[Session started at 2008-06-29 22:20:53 +0800.]

ZeroLink: unknown symbol ‘_gsl_vector_alloc’

a has exited due to signal 6 (SIGABRT).

i can check that from macports, gsl is installed well….help me…

boyfarrell says:

In versions of Xcode prior to 3.0, a property called zerolink is on by default. Search for zero link in the info window (the one where you have been adding the search paths etc.) and make sure that is off. Then clean your project and build and run. Should work.

Dan

NoFractionalReserveBanking says:

Thanks a lot for this great tutorial!
One thing, I encountered the same problem as the first person commenting on this page, so an additional thank you for solving his (and evidently mine) problem.

Keep up the great work!

Rachel says:

I also had exactly the same problem as Guillaume, so thank you for posting this tutorial. I finally have a working program. Thank you very much for such clear instructions and especially for the help in the comments. I certainly never thought release mode was part of the problem!

y says:

I’m so happy I found your site, I’ve been struggling with this for hours! THANK YOU.

Post a comment on this entry: