Just 做 IT

求知若饥 虚心若愚 안년하세요 (•‾̑⌣‾̑•)

Objective-C On Linux

2 years ago 0

Step 1: Get a Bunch of Packages

Open up a terminal session and enter the following command:


sudo apt-get install build-essential clang libblocksruntime-dev libkqueue-dev libpthread-workqueue-dev gobjc libxml2-dev libjpeg-dev libtiff-dev libpng12-dev libcups2-dev libfreetype6-dev libcairo2-dev libxt-dev libgl1-mesa-dev

Note: I am assuming that you start with a fresh Ubuntu instance, so I am listing all required packages to start from there. If you had libdispatch-dev installed previously, please remove it before proceeding with step 2. (It causes trouble when configuring GNUstep base.) We will re-install it later again.

Step 2: Download GNUstep and libobj2 Sources from the GNUstep Website

It’s important NOT to install the respective Ubuntu packages (gnustep-devel, libobjc2). Instead, download the following from the GNUstep resources site:

GNUstep Make
GNUstep Base
GNUstep GUI
GNUstep Backend
Additionally, get the latest libobjc2 sources from http://download.gna.org/gnustep/.

Finally, move the files into an install folder, and unpack them there. (I named mine ‘gnustep-dev’ and put it into my home directory.)

Step 3: Install libobjc2

Change into the unpacked libobjc2 directory and then type the following on your terminal:


export CC=clang
make
sudo make install

Note: It’s important to complete the following steps within this terminal session. Otherwise, you need to repeat export CC=clang (set the C compiler to clang instead of gcc) if you continue with a fresh session.

Step 4: Install the GNUstep Tools and Libraries

Install GNUstep Make by changing into the unpacked gnustep-make-x.x.x directory and typing:


./configure
sudo make install

Install GNUstep Base by changing into the unpacked gnustep-base-x.x.x directory and typing:


./configure
make
sudo make install

Install GNUstep GUI by changing into the unpacked gnustep-gui-x.x.x directory and typing:

./configure
make
sudo make install

Install GNUstep Backend by changing into the unpacked gnustep-back-x.x.x directory and typing:

./configure
make
sudo make install

Step 5: Install libdispatch-dev

On the terminal, type:


sudo apt-get install libdispatch-dev


Step 6: Try it out
I have set up a simple test program and named it main.m:


//
//  main.m
//  Just a little test case for Objective-C 2.0 on Ubuntu
//
//  Created by Tobias Lensing on 2/22/13.
//  More cool stuff available at blog.tlensing.org.
//
 
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import <dispatch/dispatch.h>
 
int main(int argc, const char * argv[])
{    
    @autoreleasepool {
        int multiplier = 7;
        int (^myBlock)(int) = ^(int num) {
            return num * multiplier;
        };
         
        NSLog(@"%d", myBlock(3));
 
        dispatch_queue_t queue = dispatch_queue_create(NULL, NULL); 
 
        dispatch_sync(queue, ^{
            printf("Hello, world from a dispatch queue!\n");
        });
 
        dispatch_release(queue);        
    }
 
    @autoreleasepool {
        [NSApplication sharedApplication];
        NSRunAlertPanel(@"Test", @"Wow it works!", @"OK", nil, nil);
    }
                
    return 0;
}

On the terminal, go into the directory containing main.m and execute the following command to compile the source file:


clang `gnustep-config --objc-flags` -o main -x objective-c main.m -fconstant-string-class=NSConstantString -fobjc-nonfragile-abi -fblocks -lgnustep-base -lgnustep-gui -ldispatch -I/usr/include/GNUstep -L/usr/lib/GNUstep

Finally, execute the built binary by typing:

./main

If everything works correctly, you should see the following terminal output:

2013-02-24 01:08:20.981 main[6710] 21
Hello, world from a dispatch queue!
Additionally, an alert panel should pop up as seen in the screenshot above.

Write a Comment