How To: use gcc 4.2 with Xcode 4.2
Tue. November 8, 2011Categories: Compilers, gcc-4.2, iOS, llvm, Xcode, Xcode-4.2
Tags: compiler, gcc-4.2, howto, llvm, xcode-4.2
After 3 days of trial and error, I have finally found a way to enable compiling with gcc 4.2 in xcode 4.2 that works on the iOS Simulator and iOS Devices (including 5.0). If you are looking to use GCC 4.2 in Xcode 4.2 for non-iOS Apps, see this post. This is all done via the command line, so when you see lines starting with: $
, you need to open up Terminal
(or better yet, download and use iTerm) and type the text that starts after the $
to run that command.
No files nor directories are removed nor deleted in this process, so it is easy to undo if you need to compile with LLVM in the future.
-
Download – but do not install yet – xcode_4.1_for_lion.dmg or xcode_4.1_for_snow_leopard.dmg
-
Now, follow these steps to install Xcode 4.1 into
/Developer-4.1
:-
Backup the working
/Developer
directory (where Xcode 4.2 is installed):$ sudo mv /Developer /Developer-4.2
-
Run the Xcode 4.1 installer using the default install location (
/Developer
) -
Move the new Xcode 4.1 installation to
/Developer-4.1
:$ sudo mv /Developer /Developer-4.1
-
Move the Xcode 4.2 developer directory back to
/Developer
:$ sudo mv /Developer-4.2 /Developer
-
-
Edit the Xcode 4.2
GCC 4.2.xcspec
file to get gcc 4.2 to show in the list of compiler options [1]:$ sudo vi /Developer/Library/Xcode/PrivatePlugIns/Xcode3Core.ideplugin/Contents/SharedSupport/Developer/Library/Xcode/Plug-ins/GCC\ 4.2.xcplugin/Contents/Resources/GCC\ 4.2.xcspec
-
Change lines 41 and 42 from this:
ShowInCompilerSelectionPopup = NO;
IsNoLongerSupported = YES; -
To This:
ShowInCompilerSelectionPopup = YES;
IsNoLongerSupported = NO;
-
-
Backup the Xcode 4.2 iOS/Simulator Framework
usr
directories:$ sudo mv /Developer/Platforms/iPhoneOS.platform/Developer/usr /Developer/Platforms/iPhoneOS.platform/Developer/usr.backup
$ sudo mv /Developer/Platforms/iPhoneSimulator.platform/Developer/usr /Developer/Platforms/iPhoneSimulator.platform/Developer/usr.backup -
Copy Xcode 4.1 iOS/Simulator Framework
usr
directories to Xcode 4.2:$ sudo cp -r /Developer-4.1/Platforms/iPhoneOS.platform/Developer/usr /Developer/Platforms/iPhoneOS.platform/Developer/usr
$ sudo cp -r /Developer-4.1/usr /Developer/Platforms/iPhoneSimulator.platform/Developer/usr -
Copy the
gcc
andinfo
iOS SDK library directories from Xcode 4.1 to Xcode 4.2 [2]:$ sudo cp -r /Developer-4.1/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/usr/lib/gcc /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk/usr/lib/gcc
$ sudo cp -r /Developer-4.1/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/usr/lib/info /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk/usr/lib/info -
Compile using gcc-4.2!
[1] If opening from a command line (using something like vi
, emacs
, nano
, etc) make sure to either enclose the path in quotes "/long path/with spaces/in it/file.xcspec" or escape the spaces /some/long\ path/with\ spaces/in\ it/file.xcspec
[2] This is necessary because the iPhoneOS.platform SDK has its own seperate /usr/lib
directories but the iPhoneSimulator.platform SDK does not
First of all, thanks for your great work. I’ve completed all steps, that you’ve described and I’ve actually got GCC 4.2 compiler now. But I can’t compile anything using it, even sample Single View Project. In every file I get: GCC 4.2 error. Error writing to -: Broken pipe. Do you have any ideas about this issue? Thanks in advance.
Try restarting Xcode and your device (iPhone/iPad). It sounds like Xcode cannot communicate with the device you are using to test.
Hi, everyone.
Hi, HozBlog.
[How to use gcc 4.2 with Xcode 4.2] seems like the best article to use gcc in Xcode 4.2.
I’ve worked all steps and got GCC 4.2 compiler option.
How nice it is……..
But when I compile simple things using GCC, [Error writing to -: Broken pipe] happens to me.
I have been wrestling with this error message and I have not found the solution out yet.
Some one who knows it, Please tell me about the solution.
I am looking forward to hearing from you.
There is a problem with the connection from xcode/gbd to your device. Double check your ~/.gdbinit file , make sure your device is enabled for that provisioning profile, and also make sure your iTunes and Xcode are the newest versions. I don’t know if this will work for jailbroken iphones either, but I have not tried debugging through Xcode via gdb on a jb device yet.
I am getting the following build error when starting a new project after doing the steps above:
Parse Issue Group
/Developer/Test/Test/AppDelegate.h:15:12: error: unknown property attribute ‘strong’ [1]
/Developer/Test/Test/AppDelegate.h:15:12: error: unknown property attribute ‘strong’ [1]
/Developer/Test/Test/AppDelegate.h:17:12: error: unknown property attribute ‘strong’ [1]
/Developer/Test/Test/AppDelegate.h:17:12: error: unknown property attribute ‘strong’ [1]
/Developer/Test/Test/main.m:15:5: error: unexpected ‘@’ in program [1]
To fix this you would want to change the keyword
strong
toretain
in yourAppDelegate
header file. Then, in yourmain.m
, change this:To this:
These changes are necessary because this new
@autorelease
andstrong
syntax only works with the llvm compiler and is not valid according to gcc-4.2. Xcode 4.2 templates are all written for the default llvm compiler, but they can be modified. You can check out my post on customizing Xcode template files for more details on that.Nice work, thank you. I noticed you provided a direct download link to xcode 4.1, do you happen to have a link to xcode 4.0.1 for lion?
I don’t know that one exists. I think Lion was released after Xcode 4.0.1 so they never made a specific build of that for Lion, but here is a link to Xcode 4.0.1 for Snow Leopard. A lot of other downloads are also available here: https://developer.apple.com/downloads/
There was Xcode 4.0.1 for Lion shortly available – had it installed. I lost the drive and I can’t find it anywhere. I tried the link you provided and it had a ton of torrents but no luck…
Hmm, it may be hard to find if Apple no longer provides a download link for it; but I’ll look around and let you know if I do come across it.
Thanks!