2012年12月4日火曜日

I'm back!


Some of you sharp people out there might have noticed. The title of the blog has changed. The first objective with this blog was to write about experiences from developing iOS apps using Objective-C. I have now broaden the content a bit to even include development in Objective-C on other platforms. For example by using the open source implementation of Cocoa, GNUstep, you can develop apps easily on Mac, Windows, Linux and *BSD in a platform independent environment.

First off, I want to show you how to get Obj-C 2.0 (@properties, new literals etc.) and GCD (Grand Central Dispatch) up and running on FreeBSD using GNUstep framework and some other libraries.

Let's say you've installed FreeBSD 9.x and have the system up and running. Here's my recipe for success:

- Install LLVM development package
# pkg install llvm-devel
Note: In FreeBSD 9.1 RC2/3 llvm-ld is missing so you have rebuild the kernel+world with "WITH_CLANG_EXTRAS=YES" in src.conf or use the gcc linker. Don't know if the missing llvm-files will come back for the final release or not...

- Install GNUstep and libobjc2 from tarballs
First install from ports to get all dependencies, then delete gnustep packages and install tarballs. Do this as root, not with sudo. GNUstep from ports does not support the new libobjc2 so we can't use it for development.
# pkg install gmake
# cd /usr/ports/devel/gnustep && make install clean
# [delete gnustep packages]

Download gnustep-make, -base, -gui and -backend from http://www.gnustep.org/resources/downloads.php.

Download latest libobjc2 from http://download.gna.org/gnustep/.

Start with installing gnustep-make
# [unpack gnustep-make and cd into directory]
# ./configure --with-layout=gnustep --prefix=/usr/local/GNUstep --enable-objc-nonfragile-abi CC=clang CXX=clang++
# gmake install CC=clang CXX=clang++
# . /usr/local/GNUstep/System/Library/Makefiles/GNUstep.sh (source the script, add this line to .bashrc or similar script)

libobjc2
# [unpack libobjc2 and cd into directory]
# gmake install CC=clang CXX=clang++

- Reinstall gnustep-make as above to configure it for new libobjc2

For gnustep-base, gnustep-gui, gnustep-back
# [unpack tarball and cd into directory]
# ./configure CC=clang CXX=clang++
# gmake install CC=clang CXX=clang++

GCD - Grand Central Dispatch
# cd /usr/ports/devel/libdispatch && make install clean
Include <dispatch/dispatch.h> to use.

And that's it. We will use two files to test our newly installed framework and libraries.
HelloWorld.m and the makefile GNUmakefile.

The sample code will test the new literals like @autorelease, @[ ... ], blocks and the dispatch library (launching blocks of code in different thread and with time delay).

HelloWorld.m start

#import <Foundation/Foundation.h>
#import <dispatch/dispatch.h>
#import <unistd.h>

int main (int argc, const char *argv[])
{

  @autoreleasepool {

    NSLog(@"Hello, World!");

    NSArray *a = @[ @"A", @"B", @"C" ];
    [a enumerateObjectsUsingBlock:^(NSString *s, NSUInteger index, BOOL *stop) {
      NSLog(@"String with index %d has value %@",(int)index, s);
    }];


    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_global_queue(0,0), ^(void){
             NSLog(@"Hello from delayed dispatch!");
           });
 
    dispatch_async(dispatch_get_global_queue(0,0), ^{
             NSLog(@"Hello from dispatch!");
           });
  }
  sleep(3);
  return 0;
}


HelloWorld.m end


GNUmakefile start

ifeq ($(GNUSTEP_MAKEFILES),)
 GNUSTEP_MAKEFILES := $(shell gnustep-config --variable=GNUSTEP_MAKEFILES 2>/dev/null)
endif
ifeq ($(GNUSTEP_MAKEFILES),)
 $(error You need to set GNUSTEP_MAKEFILES before compiling!)
endif

include $(GNUSTEP_MAKEFILES)/common.make

ADDITIONAL_OBJC_LIBS += -ldispatch

#
# Application
#
VERSION = 0.1
PACKAGE_NAME = HelloWorld
APP_NAME = HelloWorld
HelloWorld_APPLICATION_ICON =

#
# Other sources
#
HelloWorld_OBJC_FILES += \
HelloWorld.m

#
# Makefiles
#
-include GNUmakefile.preamble
include $(GNUSTEP_MAKEFILES)/aggregate.make
include $(GNUSTEP_MAKEFILES)/application.make
-include GNUmakefile.postamble


GNUmakefile end

Put these two files in a folder and compile using

$ gmake

This will produce the bundle HelloWorld.app (which is basically just a folder)

Launch the app by typing
$ openapp ./HelloWorld

Have fun and good luck with Objective-C 2.0 and GNUstep :)




Cocoa on FreeBSD

I'm back!


Some of you sharp people out there might have noticed. The title of the blog has changed. The first objective with this blog was to write about experiences from developing iOS apps using Objective-C. I have now broaden the content a bit to even include development in Objective-C on other platforms. For example by using the open source implementation of Cocoa, GNUstep, you can develop apps easily on Mac, Windows, Linux and *BSD in a platform independent environment.

First off, I want to show you how to get Obj-C 2.0 (@properties, new literals etc.) and GCD (Grand Central Dispatch) up and running on FreeBSD using GNUstep framework and some other libraries.

Let's say you've installed FreeBSD 9.x and have the system up and running. Here's my recipe for success:

- Install LLVM development package
# pkg install llvm-devel
Note: In FreeBSD 9.1 RC2/3 llvm-ld is missing so you have rebuild the kernel+world with "WITH_CLANG_EXTRAS=YES" in src.conf or use the gcc linker. Don't know if the missing llvm-files will come back for the final release or not...

- Install GNUstep and libobjc2 from tarballs
First install from ports to get all dependencies, then delete gnustep packages and install tarballs. Do this as root, not with sudo. GNUstep from ports does not support the new libobjc2 so we can't use it for development.
# pkg install gmake
# cd /usr/ports/devel/gnustep && make install clean
# [delete gnustep packages]

Download gnustep-make, -base, -gui and -backend from http://www.gnustep.org/resources/downloads.php.

Download latest libobjc2 from http://download.gna.org/gnustep/.

Start with installing gnustep-make
# [unpack gnustep-make and cd into directory]
# ./configure --with-layout=gnustep --prefix=/usr/local/GNUstep --enable-objc-nonfragile-abi CC=clang CXX=clang++
# gmake install CC=clang CXX=clang++
# . /usr/local/GNUstep/System/Library/Makefiles/GNUstep.sh (source the script, add this line to .bashrc or similar script)

libobjc2
# [unpack libobjc2 and cd into directory]
# gmake install CC=clang CXX=clang++

- Reinstall gnustep-make as above to configure it for new libobjc2

For gnustep-base, gnustep-gui, gnustep-back
# [unpack tarball and cd into directory]
# ./configure CC=clang CXX=clang++
# gmake install CC=clang CXX=clang++

GCD - Grand Central Dispatch
# cd /usr/ports/devel/libdispatch && make install clean
Include <dispatch/dispatch.h> to use.

And that's it. We will use two files to test our newly installed framework and libraries.
HelloWorld.m and the makefile GNUmakefile.

The sample code will test the new literals like @autorelease, @[ ... ], blocks and the dispatch library (launching blocks of code in different thread and with time delay).

HelloWorld.m start

#import <Foundation/Foundation.h>
#import <dispatch/dispatch.h>
#import <unistd.h>

int main (int argc, const char *argv[])
{

  @autoreleasepool {

    NSLog(@"Hello, World!");

    NSArray *a = @[ @"A", @"B", @"C" ];
    [a enumerateObjectsUsingBlock:^(NSString *s, NSUInteger index, BOOL *stop) {
      NSLog(@"String with index %d has value %@",(int)index, s);
    }];


    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_global_queue(0,0), ^(void){
             NSLog(@"Hello from delayed dispatch!");
           });
 
    dispatch_async(dispatch_get_global_queue(0,0), ^{
             NSLog(@"Hello from dispatch!");
           });
  }
  sleep(3);
  return 0;
}


HelloWorld.m end


GNUmakefile start

ifeq ($(GNUSTEP_MAKEFILES),)
 GNUSTEP_MAKEFILES := $(shell gnustep-config --variable=GNUSTEP_MAKEFILES 2>/dev/null)
endif
ifeq ($(GNUSTEP_MAKEFILES),)
 $(error You need to set GNUSTEP_MAKEFILES before compiling!)
endif

include $(GNUSTEP_MAKEFILES)/common.make

ADDITIONAL_OBJC_LIBS += -ldispatch

#
# Application
#
VERSION = 0.1
PACKAGE_NAME = HelloWorld
APP_NAME = HelloWorld
HelloWorld_APPLICATION_ICON =

#
# Other sources
#
HelloWorld_OBJC_FILES += \
HelloWorld.m

#
# Makefiles
#
-include GNUmakefile.preamble
include $(GNUSTEP_MAKEFILES)/aggregate.make
include $(GNUSTEP_MAKEFILES)/application.make
-include GNUmakefile.postamble


GNUmakefile end

Put these two files in a folder and compile using

$ gmake

This will produce the bundle HelloWorld.app (which is basically just a folder)

Launch the app by typing
$ openapp ./HelloWorld

Have fun and good luck with Objective-C 2.0 and GNUstep :)




Related Posts Plugin for WordPress, Blogger...