4

Alright, I have a rather odd question here. I feel much more comfortable writing code in Objective-C over any other language. I recently had to do some server-side programming, which required me to learn PHP. It works, yeah, but for fun I want to achieve the same thing through Objective-C. So, I created a binary using Xcode's Foundation preset. Here's most of the binary:

#import <Foundation/Foundation.h>
#import "JSONKit.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSString *theURL = [NSString stringWithFormat:@"http://blahblahblah.com/blah"];
    NSError *err = nil;
    NSURLResponse* response = nil;
    NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] init] autorelease];
    NSURL*URL = [NSURL URLWithString:theURL];
    [request setURL:URL];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [request setTimeoutInterval:30];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
    NSDictionary *someData = [data objectFromJSONData];
    NSString *someString = [[someData objectForKey:@"foo"]objectForKey:@"bar"];
    //do something

    [pool drain];
    return 0;
} 

Quite basic code. It simply downloads some stuff from my server and I parse the JSON result and get a string that I want to use. So, my question is - how can I run this on my Linux-based server? I know it's possible, maybe using GNUStep (or cocotron?), which I don't know how to use. Anyone have ideas?

sudo rm -rf
  • 29,408
  • 19
  • 102
  • 161

3 Answers3

4

Well, I suggest the same thing as the @lacqui.. Use CGI to run your program.. and here's the steps ..

(Side note: using CGI is deprecated as it starts a process each time a request is coming to the server (modern servers/web containers initiate a new thread (vs process).)

So, let's Start:

  • The input at hand is a program written in Objectiv-c
  • The output is a CGI script(program or whatever they name it) that will run inside some http server.

First, let me ask you, What's the target platform to deploy your application?

  • If target deployment platform is a Mac then you will have to get the binary out of xcode ( I think it would be in a .dmg format) and find some where how to run a .dmg as a CGI program inside a web server ( I am not sure if apache runs under Mac or not)

  • But If it is Windows or Linux:

    1. You will need to compile your application using GNUstep (I know nothing about portability from Xcode to GNUstep) You will need a GNUstep. Steps to install GNUstep either for Windows or Linux is trivial.
    2. Once Installing GNUstep, you will have to compile your application again using it, refer to the same two links above to know how to compile your application.
    3. The issue here is, AFAIK, GNUstep don't fully support Objc-2, so possibilities that the compilation will fail cause of usage of JSONKit.h is high. If your program compiles successfully, then you are almost done.
    4. Suppose your program compiles, And you now have the binary program.. You will need to deploy it in some HTTP server that have CGI enabled. You can follow my blogpost here to know how to deploy a binary program written in C into some small http server called mini-httpd on Linux (it should apply to any binary program regardless of its source language).
Muhammad Hewedy
  • 29,102
  • 44
  • 127
  • 219
  • Thank you very much for the detailed steps. I'll check it out when I get a chance. Just to clarify, I'm compiling this application on a **Mac**, and deploying to a **Linux** server. – sudo rm -rf Sep 16 '11 at 15:54
2

What you want to look at is called the Common Gateway Interface. It is a protocol that states the way a web server will interact with subordinate processes.

What will happen is that, when a user browses to the URL that is mapped to your program, the server will start your program, and put the text of the request into STDIN. Your program will do whatever processing is required, then put the results (as well as some header information) into STDOUT.

Kevin Lacquement
  • 5,057
  • 3
  • 25
  • 30
  • This isn't actually meant to do anything for a user. It's just a script that will run silently on my server doing a certain process. I'm just unable to get it running on my server. – sudo rm -rf Sep 05 '11 at 01:51
1

What goes wrong when you try? You should be able to compile it with the GCC's Objective-C compiler. You should be able to run it.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • I get lots of errors about undefined symbols for architecture x86_64. I'm assuming you mean do something like this? `gcc main.m -o someexec` – sudo rm -rf Sep 05 '11 at 01:50
  • When you see which symbols are undefined, you can track down the appropriate libraries that contain them. (GNUStep, probably.) – David Schwartz Sep 05 '11 at 02:56
  • I haven't installed GNUStep. Should I be doing that, even if I'm on a Mac? – sudo rm -rf Sep 05 '11 at 02:59
  • I thought this question was about a Linux-based server. It doesn't matter who made the hardware. – David Schwartz Sep 05 '11 at 03:20
  • Oh I'm sorry. I'm talking about compiling it on my computer, then uploading the resulting binary to my server. Isn't that what you're suggesting? I'm so confused. – sudo rm -rf Sep 05 '11 at 03:30
  • If the two computers are different platforms, compiling on one to run on the other is going to be very, very difficult. – David Schwartz Sep 05 '11 at 03:38
  • Let me clarify. I have a Mac laptop. I'm using Xcode to write this simple app in Objective-C. I was wondering if I could compile a binary that would run on my Linux-based server. I guess that's not possible? :( – sudo rm -rf Sep 05 '11 at 03:40
  • It should work, but you'll need to compile it on that platform or you'll need to set up a cross-compiler. – David Schwartz Sep 05 '11 at 04:17
  • By "that platform", do you mean my Linux server? I'm sorry I'm just not quite understanding you. ;) – sudo rm -rf Sep 05 '11 at 13:37
  • If you want to run a program on a platform, you either need to compile it on that platform or you need to use a cross-compiler. Since you're not using a cross-compiler, you need to compile on the same platform you plan to run on. – David Schwartz Sep 05 '11 at 16:38