Integrating a C command line tool into Objective-C application

  • Hello everyone,

    Firstly I'd like to say that I've learnt a lot from reading the
    discussions over the past year or so and it's helped me with my coding
    and general mind set for coding in obj-c. Thanks everyone!
    Now my question. I've read through the documentation and also done
    google searches but I am yet to find *exactly* what I'm looking for to
    help me. A colleague wrote a command line tool in C a while back and
    now I want to integrate that tool into my obj-c code. I'm not sure how
    to go about this. I started coding on obj-c about a year ago on and
    off and haven't coded in C before, but looking through some books I
    have a general idea in that area. The problem is the integration, ie
    the Main function etc. Do I make the C code a new class and strip out
    the main function? I tried using that code verbatim (minus Main) and
    tried to turn it all into a method but the compiler said I can't have
    nested functions. If anybody can help in this area, I would be most
    appreciative.

    Regards,
    Jason Wiggins
  • well, the proper-proper solution is to make a "static library" out of
    that command-line application and link your cocoa-app to that library.
    This way it would be possible to change (later) that library and both
    of your apps would be up-to-date (after relinking)

    but, anyway, your problem seem to be related to some compiler flags
    (or, some typo)

    On 9/8/07, Jason Wiggins <jwiggins...> wrote:
    > Hello everyone,
    >
    > Firstly I'd like to say that I've learnt a lot from reading the
    > discussions over the past year or so and it's helped me with my coding
    > and general mind set for coding in obj-c. Thanks everyone!
    > Now my question. I've read through the documentation and also done
    > google searches but I am yet to find *exactly* what I'm looking for to
    > help me. A colleague wrote a command line tool in C a while back and
    > now I want to integrate that tool into my obj-c code. I'm not sure how
    > to go about this. I started coding on obj-c about a year ago on and
    > off and haven't coded in C before, but looking through some books I
    > have a general idea in that area. The problem is the integration, ie
    > the Main function etc. Do I make the C code a new class and strip out
    > the main function? I tried using that code verbatim (minus Main) and
    > tried to turn it all into a method but the compiler said I can't have
    > nested functions. If anybody can help in this area, I would be most
    > appreciative.
    >
    > Regards,
    > Jason Wiggins
    >

    --
    Alexey Zakhlestin
    http://blog.milkfarmsoft.com/
  • On 08.09.2007, at 09:08, Jason Wiggins wrote:
    > Now my question. I've read through the documentation and also done
    > google searches but I am yet to find *exactly* what I'm looking for
    > to help me. A colleague wrote a command line tool in C a while back
    > and now I want to integrate that tool into my obj-c code. I'm not
    > sure how to go about this. I started coding on obj-c about a year
    > ago on and off and haven't coded in C before, but looking through
    > some books I have a general idea in that area. The problem is the
    > integration, ie the Main function etc. Do I make the C code a new
    > class and strip out the main function? I tried using that code
    > verbatim (minus Main) and tried to turn it all into a method but
    > the compiler said I can't have nested functions. If anybody can
    > help in this area, I would be most appreciative.

    I hope I'm not explaining too basic things to you, but I'm just
    trying to cover all places where you could be hung up:

    A function in C is essentially the same as a function in ObjC, e.g.
    NSLog() is a function. Some C compilers let you put one function
    *definition* (1) inside another, which is what is called "nested
    functions". This is a nonstandard extension that e.g. GCC 3.3 would
    let you do it. GCC 4.x doesn't let you do nested functions anymore (2).

    Methods are very similar to functions. Essentially, they are
    functions associated with a class of object. If you try to put a
    function inside a method, you'll probably get the same error message
    as when you try to put one function inside the other.

    Something like this must be going on in the code you created based on
    your colleague's code. Either you copy-pasted a group of functions
    into one of your methods, or your colleague used nested functions.

      There are two approaches I usually take when converting other
    people's C code to ObjC:

    1) Take the whole C code and just write a function that effectively
    contains the code from the "main" function, but with nicer
    parameters, and then call that C function wherever I need.

    2) If I want to replace some code, or the code calls back to my code
    (in C this is usually done by passing a pointer to one function to
    another), I just turn the whole package into one class, and make
    every function a method. The callback functions get turned into
    methods that simply call through to a delegate I add to instances of
    this class.

    Of course, if I have to keep the original C code and my Objective C
    code synched, I usually take a hybrid approach, where I generally
    just do #1, but create an ObjC class that calls the function that was
    based on the "main" function, which installs callback functions that
    just call through to a delegate.

    Cheers,
    -- M. Uli Kusterer
    http://www.zathras.de

    1) A function definition is essentially the code that makes up the
    actual function. This is different from a function call, which is
    what most function definitions use to actually do their work.

    2) The way GCC implemented nested functions was taking advantage of a
    security gap in the OS, which Apple intends to plug, so they removed
    this feature so people can prepare. Nested functions are an
    extension, not part of ANSI standard C or ISO C, so the complaints
    weren't too loud.
  • On 08/09/2007, at 7:15 PM, Uli Kusterer wrote:
    >
    > I hope I'm not explaining too basic things to you, but I'm just
    > trying to cover all places where you could be hung up:

    Basic is good as I'm learning this "new" stuff.
    >
    > A function in C is essentially the same as a function in ObjC, e.g.
    > NSLog() is a function. Some C compilers let you put one function
    > *definition* (1) inside another, which is what is called "nested
    > functions". This is a nonstandard extension that e.g. GCC 3.3 would
    > let you do it. GCC 4.x doesn't let you do nested functions anymore
    > (2).
    >
    > Methods are very similar to functions. Essentially, they are
    > functions associated with a class of object. If you try to put a
    > function inside a method, you'll probably get the same error message
    > as when you try to put one function inside the other.
    >
    > Something like this must be going on in the code you created based
    > on your colleague's code. Either you copy-pasted a group of
    > functions into one of your methods, or your colleague used nested
    > functions.

    That's what I did, just copy and paste job, just to see if it would
    work, but no.
    >
    >
    > There are two approaches I usually take when converting other
    > people's C code to ObjC:
    >
    > 1) Take the whole C code and just write a function that effectively
    > contains the code from the "main" function, but with nicer
    > parameters, and then call that C function wherever I need.
    >
    > 2) If I want to replace some code, or the code calls back to my code
    > (in C this is usually done by passing a pointer to one function to
    > another), I just turn the whole package into one class, and make
    > every function a method. The callback functions get turned into
    > methods that simply call through to a delegate I add to instances of
    > this class.
    >
    > Of course, if I have to keep the original C code and my Objective C
    > code synched, I usually take a hybrid approach, where I generally
    > just do #1, but create an ObjC class that calls the function that
    > was based on the "main" function, which installs callback functions
    > that just call through to a delegate.
    >
    > Cheers,
    > -- M. Uli Kusterer
    > http://www.zathras.de
    >
    > 1) A function definition is essentially the code that makes up the
    > actual function. This is different from a function call, which is
    > what most function definitions use to actually do their work.
    >
    > 2) The way GCC implemented nested functions was taking advantage of
    > a security gap in the OS, which Apple intends to plug, so they
    > removed this feature so people can prepare. Nested functions are an
    > extension, not part of ANSI standard C or ISO C, so the complaints
    > weren't too loud.
    >

    Thanks for the info, I'll have to read it a few times to let it sink
    in. I'll take what you have provided and give it a go.

    Regards,

    Jason Wiggins
  • > Hello everyone,
    >
    > Now my question. I've read through the documentation and also done
    > google searches but I am yet to find *exactly* what I'm looking for
    > to help me. A colleague wrote a command line tool in C a while back
    > and now I want to integrate that tool into my obj-c code. I'm not
    > sure how to go about this. I started coding on obj-c about a year
    > ago on and off and haven't coded in C before, but looking through
    > some books I have a general idea in that area. The problem is the
    > integration, ie the Main function etc. Do I make the C code a new
    > class and strip out the main function? I tried using that code
    > verbatim (minus Main) and tried to turn it all into a method but
    > the compiler said I can't have nested functions. If anybody can
    > help in this area, I would be most appreciative.
    >

    If the commandline tool is not too complex is its interaction with
    the user (e.g. it simply takes some arguments and produces output on
    stdout), you can also include it in your bundle by dragging the
    executable to the resources group in the XCode sidebar. Then you can
    use NSTask to run it and NSPipe and NSFileHandle to input and output
    data.

    For a tutorial, see: http://cocoadevcentral.com/articles/000025.php

    Cheers, Patrick
previous month september 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today