Programming: Run app via programming command

Discussion in 'Mac Programming' started by slooksterPSV, Jul 7, 2006.

  1. macrumors 68030

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #1
    Is there anyways to make your application run an external application? Ok so I want to make a lightweight compiler for C, C++, HTML, PHP (the latter 2 I know don't need a compiler, but still). Is there anyways I can run an application from within an application? Mainly gcc and/or g++ ?
    Is there a way to do it with Objective-C something like this?

    [NSApplication runApplication: @"/usr/bin/gcc"]
     
  2. macrumors 68020

    ham_man

    Joined:
    Jan 21, 2005
    #2
    XCode does it. SuperDuper does it with psync. I don't see why you couldn't do it. You may need to dig for the proper classes and whatnot, but it is possible.

    *shrugs*
     
  3. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    Take a look at NSTask.

    There is a step-by-step example of how to wrap a command line app here.
     
  4. thread starter macrumors 68030

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #4
    Here it is, I got it to work like this:
    ... //code up here
    NSString *launchProg = @"/usr/bin/gcc";

    NSArray *parameters;
    parameters = [NSArray arrayWithObjects: @"/Users/sbarn/Programming/test.c", @"-o", @"/Users/sbarn/Programming/MyLongApp", nil];

    NSTask *nts = [[NSTask alloc] init];
    [nts setLaunchPath: launchProg];
    [nts setArguments: parameters];
    [nts launch];
    ... //more code down here
     
  5. macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #5
    couldn't this be done using the system() function in C?
     
  6. thread starter macrumors 68030

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #6
    It could, but I want to do things in pure Obj-C & Cocoa where possible.
     
  7. macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #7
    Actually, using the system() function IS pure Objective C. Objective C is a pure superset of C, so every command valid in C, is pure Objective C too. Plus, it's easier to do it that way, so why go into your toilet by visiting Italy first?
     
  8. thread starter macrumors 68030

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #8
    I like using the SmallTalk Syntax with [ ], it just makes things easier and more readable.
     
  9. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #9
    You can also do it using the various exec* and fork* functions, which is the typical UNIX way.
     

Share This Page