Setting Xcode’s default compiler for a project

(UPDATE: From what I can see, Apple have made the reasons for this tutorial moot by changing the default compiler to GCC 4.2 for C tools, in Xcode 3.2 (Snow Leopard). Certainly, if you are still using an earlier version of Xcode, or you are interested in finding out a little about build settings in Xcode projects, read on. I also note that the default C dialect is the C99 standard, though it may well have been this way before I upgraded and I just didn’t notice.)

There are many reasons to change the compiler used by Xcode, most of which I’m probably unaware of. However, I’ve been informed by a reliable source that there are indeed some good reasons for doing so, and for my own purposes I needed to use the latest version of the GNU GCC compiler possible.

Note: This article applies to Xcode 3.1.2, in Mac OS X 10.5.7 Leopard.

Getting started

If you don’t have the Xcode developer tools installed (by default located under /Developer) , you can install them from your OS X install discs, or download the latest version.

You can test that the GNU GCC compiler is installed by launching Terminal and typing gcc --version. By default, the output should be something like this:

i686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5490)
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

What versions are available?

The default GCC version for any OS X system is 4.0.1; also available are 4.2.1, 3.3 and LLVM-GCC 4.2. A good explanation of these, and why you might choose each one, can be found here.

(Later on you’ll see that there is one other option presented in Xcode—it’s labelled GCC System Version, and I assume that this option just selects your system’s default. This is an area that began to confuse me greatly when I started exploring it! If you’re interested in modifying this, check out MacPorts and in particular the gcc_select port. I haven’t succeeded yet, but I’m pretty sure that’s how you’d change it. Please feel free to comment below if you know in more detail!)

Selecting your compiler in Xcode

After creating a C/C++ project, select the project file in the Groups & Files pane on the left of the Xcode window.

groups-and-files

Next, click the Get Info button in the Toolbar to bring up the Project Inspector. Click on the Build tab, and you’ll be presented with all of the available build options. We’re only interested in one, so type “compiler” into the search field and the results will be narrowed sufficiently.

project-inspector-build

You can see above that I’ve already selected GCC 4.2 as the compiler version for this project. To change it, just click the version listed and a menu appears from which you can select the desired version. Easy!

Using GCC 4.2.1 from the command line

If you’d like to configure the the gcc command to automatically use version 4.2.1, type the below commands in a Terminal window:

cd /usr/bin
sudo ln -sf gcc-4.2 gcc
sudo ln -sf g++-4.2 g++

Try gcc --version again and you should see the results. Alternatively, have a look at this thread on Apple’s discussion boards for a bit more info.

UPDATE (July 31, 2009): If you’re having any trouble with these links not working once you’re in a new Terminal session, I’ve got another workaround. For myself, the output of gcc --version would still show that I was using GCC 4.0.1, though a directory listing of /usr/bin indicated that the link here was pointing to GCC 4.2.1. Yet, if entered the command /usr/bin/gcc --version, I would get the expected output. I could not figure out what may have been wrong with my PATH variable, so to fix this I added this line to my .bash_profile file in my home directory:

alias gcc="/usr/bin/gcc"

To check if you have a .bash_profile, make sure you type ls -a in a Terminal session—you won’t be able to see it in the Finder. If the file doesn’t exist, you can create it with any text editor. (Note: Once you save the file, you generally won’t be able to open it again from a standard Open dialogue. Just type open .bash_profile in Terminal and it should launch TextEdit for editing.)

Also, thanks to this article at the Lap Cat Software Blog, I now know how to change the default build settings in a project.

Once I found the Project Templates directory, the rest was easy. In Xcode 3.1, templates are located under /Developer/Library/Xcode/Project Templates. They are filed under the same headings that you see when you select New Project… from Xcode’s File menu.

In my case I wanted to change the settings for a Standard Tool (a C command line tool). To do this I navigated to Command Line Utility/Standard Tool under the Project Templates directory. For any project, what you’ll see in this folder are the default files for new projects of this type. This includes a .xcodeproj file, which contains the settings I’m interested in.

First I made a backup of the file, and then launched it, which opens what appears to be a standard Xcode project. By simply following the instructions listed above, I changed the compiler version, and closed Xcode (it isn’t necessary to save—in fact, you can’t). Delete the Build directory that was created by Xcode, and next time you create a project of that type you’ll have the new default settings applied—voilà!

This entry was posted in Computing, Programming, Xcode and tagged , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe without commenting