Search code examples
cprogram-entry-point

C99: Call main function from another 'main'?


This question has a similar title but we are discussing different things.

Say I want to invoke the main of a code foo.c

int main(){
   ...
}

The problem is that the caller is another main routine, and it seems impossible to have two main functions.

How can I call foo.c's main function from another main?

The name of the main in foo.c cannot be changed, because it is not a user-code.


Solution

  • The situation you are describing sounds something like this:

    /**
     * bar.c
     */
    ...
    int main( void )
    {
      ...
      // call foo.c's main function directly
      main( );
      ...
    }
    
    /**
     * foo.c
     */
    int main( void )
    {
      ...
    }
    

    You can't do that. There's no way to build a program that has two main functions defined.

    So the question becomes, what are you really trying to do here?

    1. Are bar.c and foo.c meant to be built into the same executable, with code in bar.c calling code defined within foo.c, while still allowing foo.c to be built as a standalone program? If so, then you'll have to take that code out of foo.c's main function and put it in a separate function, and you'll have to use some preprocessor magic to suppress foo.c's main definition when both files are built together, something like this:
      /**
       * bar.c
       */
      #include "foo.h"
      ...
      int main( void )
      {
        ...
        foo_func(); // function defined in foo.c
        ...
      }
      
      /**
       * foo.h
       */
      #ifndef FOO_H
      #define FOO_H
      
      void foo_func( void );
      #endif
      
      /**
       * foo.c
       */
      #include "foo.h"
      
      void foo_func( void )
      {
        ...
      }
      
      #ifndef EXTERNAL_DRIVER
      int main( void )
      {
        ...
        foo_func();
        ...
      }
      #endif
      
      This allows `foo` to be built as a standalone program, as well as part of a larger program. To build it as part of a larger program, you must define the `EXTERNAL_DRIVER` macro, something like this:
      gcc -o bar bar.c foo.c -DEXTERNAL_DRIVER
    2. Are bar.c and foo.c meant to be built into two separate executables, and have a running bar instance start a new foo instance, then wait for the foo instance to finish running before moving on? If so, then you can use the C standard library's system function to invoke foo from bar:
      system("foo");
      although you may have to specify the path as part of the command, such as
      system("./foo")
      or
      system("/some/absolute/path/name/foo");
      If you're on a *nix system, you can use a combination of fork and one of the exec* functions to do much the same thing, except that the bar instance doesn't have to wait for foo to finish executing. If you want the two programs to share data while running, then you'll have to look into various interprocess communication techniques (shared memory, sockets, pipes, etc.).
    3. . Are bar.c and foo.c meant to be built into two separate executables, and have a running bar instance execute code defined within a running foo instance? That's a remote procedure call, which I've done maybe once in the last 20 years and, like interprocess communication, is a bit involved for a SO answer.

    If I haven't covered your particular use case, let me know.