Calling 'foo.c' or 'foo2.c' from my 'main_code.c'

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Francesco Moi

    #1

    Calling 'foo.c' or 'foo2.c' from my 'main_code.c'

    Hello.

    I don't know if following is possible.

    I've got 'main_code.c':
    ............... .............
    char * another_code;
    another_code = "foo.c";
    ............... .............

    I've got 'foo.c':
    ............... .............
    #include <stdio.h>
    int main()
    {
    printf ("Hello world from 'foo.c'!\n");
    return 0;
    }
    ............... .............

    I would like to call 'foo.c' main function from 'main_code.c'.

    Is this possible?

    Thank you very much and best regards.
  • Joona I Palaste

    #2
    Re: Calling 'foo.c' or 'foo2.c' from my 'main_code.c'

    Francesco Moi <francescomoi@e urope.com> scribbled the following:[color=blue]
    > Hello.[/color]
    [color=blue]
    > I don't know if following is possible.[/color]
    [color=blue]
    > I've got 'main_code.c':
    > ............... ............
    > char * another_code;
    > another_code = "foo.c";
    > ............... ............[/color]
    [color=blue]
    > I've got 'foo.c':
    > ............... ............
    > #include <stdio.h>
    > int main()
    > {
    > printf ("Hello world from 'foo.c'!\n");
    > return 0;
    > }
    > ............... ............[/color]
    [color=blue]
    > I would like to call 'foo.c' main function from 'main_code.c'.[/color]
    [color=blue]
    > Is this possible?[/color]
    [color=blue]
    > Thank you very much and best regards.[/color]

    If you link the object files compiled from main_code.c and foo.c
    together, all you have to do is call main(). Otherwise, if they are
    linked into separate executables, you essentially have to use
    system() to call the other executable via the OS. This will, of
    course, require using the name of the executable, not of its source.
    If you want to call a specific function (not necessarily main()),
    you can try to find a system-specific IPC (Inter-Process Communication)
    feature from your OS, or implement a C interpreter.

    --
    /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "You can pick your friends, you can pick your nose, but you can't pick your
    relatives."
    - MAD Magazine

    Comment

    • Malcolm

      #3
      Re: Calling 'foo.c' or 'foo2.c' from my 'main_code.c'


      "Francesco Moi" <francescomoi@e urope.com> wrote in message[color=blue]
      >
      > I don't know if following is possible.
      >
      > I've got 'main_code.c':
      >
      > I would like to call 'foo.c' main function from 'main_code.c'.
      >[/color]
      Not in standard ANSI C. Only one main() function is allowed which is the
      program start point. You need to rename the "main" in foo.c as something
      else (eg foomain).


      Comment

      • Christopher Benson-Manica

        #4
        Re: Calling 'foo.c' or 'foo2.c' from my 'main_code.c'

        Malcolm <malcolm@55bank .freeserve.co.u k> spoke thus:
        [color=blue]
        > Not in standard ANSI C. Only one main() function is allowed which is the
        > program start point. You need to rename the "main" in foo.c as something
        > else (eg foomain).[/color]

        Is it correct to say that your statements apply to hosted
        implementations only?

        --
        Christopher Benson-Manica | I *should* know what I'm talking about - if I
        ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

        Comment

        • Malcolm

          #5
          Re: Calling 'foo.c' or 'foo2.c' from my 'main_code.c'


          "Christophe r Benson-Manica" <ataru@nospam.c yberspace.org>[color=blue]
          >
          > Is it correct to say that your statements apply to hosted
          > implementations only?
          >[/color]
          A non-hosted implementation may have a start point other than main(). It may
          even allow main() as a normal function name, so you could link in foo.c with
          a main(), once, when you try to link bar.c with it's main() you're back to
          square one.


          Comment

          Working...