Clang and Objective-C on Windows
Posted on March 21, 2012 by Paul
UPDATE 17 May 2014
Newer versions of Clang and LLVM seems to have a problem with GNUstep dev tools which are based on the now old gcc-4.6.1 compiler. I’ve changed the post to avoid this problem by using r181679 which works with GNUstep.
UPDATE 13 May 2013
For your convenience, I’ve uploaded on github a binary version of Clang and GNUstep. My recommendation is to compile it yourself as described in the article. If you need a quick way to use Clang and GNUSstep, you can use the linked binary: https://github.com/sol-prog/Clang_GNUstep_Objective-C_for_Windows
If you want to start learning Objective-C on a Windows computer, you’ve come to the right place. This tutorial will show you how to install a compiler and the necessary frameworks to start hacking Objective-C on Windows today. Be warned that I’m not talking about developing the next Objective-C iPhone/iPad application on Windows, this is not possible at the time of this writing!
A prerequisite for a successful install is the Python interpreter. Go to http://www.python.org/ and install Python 2.7.x on your machine. After that you will need to update your path to include C:\Python27.
We’ll start with installing GNUstep, which aims to be “a free and open version of the Cocoa (formerly known as NeXTSTEP/OpenStep) APIs and tools”. In the next step, we will install Clang and I will show you how to write and compile a small Objective-C program.
Navigate to GNUstep Windows, download and install in this order: GNUstep MSYS System, GNUstep Core, GNUSstep Devel. From now on I will assume that you’ve installed GNUstep in C:\GNUstep. If you’ve installed GNUstep on a different location, please replace every occurrence of C:\GNUstep in the next steps accordingly.
GNUstep comes with the GCC compiler which, in principle, can be used to compile Objective-C programs. However in the last years Apple has added some important extensions to Objective-C, that are not available with gcc. Apple’s default compiler is Clang which is able to compile any modern Objective-C code. Since Clang doesn’t have an official Windows installer at the time of this writing we are going to compile it from sources.
Open a GNUstep shell window, you can find this in Start-GNUstep-Shell and write/paste the next lines one by one:
Now, we need to edit some hard coded paths from clang. Without this, clang won’t be able to find your header files. Navigate to your HOME folder, which on my machine is on C:\GNUstep\msys\1.0\home\sol, in your case this should something like C:\GNUstep\msys\1.0\home\name. Enter in llvm-tools-clang-lib-Frontend and open in your favorite text editor (I recommend Notepad++ or SublimeText 2) InitHeaderSearch.cpp. Search for the line that starts with “FIXME: temporary hack: hard-coded paths.” at about line 215 and add these lines:
Don’t forget to comment line 11 in the above. Save and close the file. Next, we will create a build directory and we will configure the code for compilation:
Now, we are ready to compile Clang, this will take some time:
Please note that in the above I’ve explicitly chose to compile the code on a single processor, keep the make line this way.
Let’s check if clang works:
You should see something like:
Let’s try to compile a small Objective-C code in order to test our new compiler and libraries. Please note that this example won’t work with GCC and GNUstep, it will, however compile with Clang and GNUstep.
Create a new folder in your GNUstep HOME and in this folder two files: GNUmakefile and test.m:
Open test.m in your text editor, copy and save the next Objective-C code:
Copy and save the next code in GNUmakefile:
Now, you are ready to compile your first Objective-C program on Windows:
In the above I’ve specifically instructed make to use Clang as the default compiler.
You can run your executable with: