Compile Objective-C using GNUstep on Non-Mac OS
For those who want to play with Objective-C without having a mac (+ XCode IDE), GNUstep is the way to go. It enables you to develop programs in Objective-C on many platforms like *nix and even Windows. This time, I installed GNUstep on debian, and compiled and ran some Objective-C programs.
What’s GNUstep?
Probably you may know Apple’s Cocoa , Objective-C developmet frameworks(was known as NeXTSTEP many years ago). GNUstep is a GNU clone which not only is compatible with the Cocoa but also supports cross platform!
GNUstep provides a robust implementation of the AppKit and Foundation libraries as well as the development tools available on Cocoa, including Gorm (the InterfaceBuilder) and ProjectCenter (ProjectBuilder/Xcode). GNUstep currently supports Unix (GNU/Linux and GNU/HURD, Solaris, NetBSD, OpenBSD, FreeBSD, Darwin) and Windows. — GNUstep Overview –
Installing GNUstep on debian
First of all install the following 2 debian packages which are very fundamental for GNUstep development. The version of debian on which I tried it is 5.0.4.
- gnustep – The GNUstep Development Environment — user applications
- gnustep-devel – The GNUstep Development Environment — development tools
$ sudo apt-get install gnustep-devel
If you are not debian user, find out GNUstep packages for the platforms you use from here.
Compiling Objective-C code with GNUstep
main.m
int main(int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *noncap = @"how do you like wednesday?";
NSString *cap;
cap = [noncap capitalizedString];
NSLog(@"output = %@", cap);
[pool release];
return 0;
}
I coded a simple program above that captalize a string “how do you like wednesday?”. Now I want to compile it. But before that, make sure that gcc (>4.6) is installed on your platform. If not installed, just install it.
1. Compiling by executing gcc command line
gnustep-config is a usuful command that provides information on the current gnustep installation. gnustep-config’s –objc-flags option prints all the flags required to compile an ObjC file, and –objc-libs option prints all the flags required to link a GUI ObjC program.
-MMD -MP -DGNUSTEP -DGNUSTEP_BASE_LIBRARY=1 -DGNU_GUI_LIBRARY=1 -DGNU_RUNTIME=1
-DGNUSTEP_BASE_LIBRARY=1 -D_REENTRANT -fPIC -g -Wall -DDEBUG -fno-omit-frame-pointer
-DGSWARN -DGSDIAGNOSE -Wno-import -g -fno-strict-aliasing -fexceptions
-fobjc-exceptions -D_NATIVE_OBJC_EXCEPTIONS -fgnu-runtime
-fconstant-string-class=NSConstantString -I. -I/home/kawasaki/GNUstep/Library/Headers
-I/usr/local/include/GNUstep -I/usr/include/GNUstep
-rdynamic -shared-libgcc -fexceptions -fgnu-runtime
-L/home/kawasaki/GNUstep/Library/Libraries -L/usr/local/lib -L/usr/lib -lpthread -lobjc -lm
2. Compiling by Makefile, a little more sophisticated way
As a different way, you can create a Makefile for the program to compile. I created the following Makefile using examples from “A GNUstep Programming Tutorial”
GNUmakefile
include $(GNUSTEP_MAKEFILES)/common.make
TOOL_NAME=SampleCode
SampleCode_HEADERS =
SampleCode_OBJC_FILES = main.m
SampleCode_RESOURCE_FILES =
include $(GNUSTEP_MAKEFILES)/tool.make
After you compile it with either way, finally you can execute the compiled program. Its output will be like this:
Enjoy Objective-C programming with GNUStep!
REFERENCES
- http://www.gnustep.org
- Cocoa Fundamentals Guide: What Is Cocoa?
- A GNUstep Programming Tutorial
- The Objective-C Programming Language
No related posts.
Posted in: Programming


