Categories
- Christianity (7)
- And Then Some (4)
- Creation (1)
- Outreach (1)
- The Church (3)
- Computing (21)
- Apple (2)
- Blogging (4)
- WordPress (3)
- Mac OS X (6)
- Programming (5)
- Text Editing (3)
- The Web (6)
- Social Media (1)
- Typography (4)
- Miscellaneous (5)
- Personal (5)
- Christianity (7)
Archives
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: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_selectport. 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.
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.
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
gcccommand to automatically use version 4.2.1, type the below commands in a Terminal window:Try
gcc --versionagain 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 --versionwould still show that I was using GCC 4.0.1, though a directory listing of/usr/binindicated 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 myPATHvariable, so to fix this I added this line to my.bash_profilefile in my home directory:To check if you have a
.bash_profile, make sure you typels -ain 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 typeopen .bash_profilein 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 Toolunder 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.xcodeprojfile, 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à!