zell
zell

Reputation: 10204

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.

Upvotes: 4

Views: 5479

Answers (7)

John Bode
John Bode

Reputation: 123538

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.

Upvotes: 4

Kijewski
Kijewski

Reputation: 26033

You can rename the main() function like that:

objcopy --redefine-sym main=that_other_main obj1.o obj2.o

Where obj.o contains the main() that you don't want to be the main entry point of your program. You can then call it with that_other_main(argc, argv).

Upvotes: 5

MAS
MAS

Reputation: 111

you can't work with two main functions.You better change the function name from 'main' to any other in foo.c and call it.I think it will not change the ultimate result of your program.

Upvotes: 0

James Anderson
James Anderson

Reputation: 27478

The best you can do is call the other program with a "system('otherprog.exe arg1 arg2')" call. That is if you don't care about the output going straight to stdout.

If you want to capture the output from the second program than you need to open a pipe with the more complex popen() call.

FILE *fp;
int status;
char res[MAXLINE];


fp = popen("otherprog.exe param1 parm2", "r");
if (fp == NULL)
    /* Handle error */;

while (fgets(res, MAXLINE, fp) != NULL) {
    /* capture each stdout line from otherprog.exe */
    printf("do something with %s", res);
}

status = pclose(fp);

Upvotes: 2

R Sahu
R Sahu

Reputation: 206667

How can I call foo.c's main function from another main? There is one constraint here. The name of the main in foo.c cannot be changed, because it is not a user-code.

You cannot have two main functions in a program.

You cannot call the main of foo.c from another function since main is the starting point of a program as far as user code is concerned.

What you are trying to accomplish can be achieved only by having two different programs and executing the program that contains the main of foo.c from the other program using system or any of the exec*** family of functions.

Upvotes: 2

blue112
blue112

Reputation: 56512

and it seems impossible to have two 'main' functions.

You can't have two symbol with the same name in the same object (binary, shared lib)...

At the end of the compilation, the compiler merges all objects files into into your target, resolving the symbol by name. It has no way to tell apart your two mains if they have the same name.

Rename one of the functions (the one which isn't the real entry point of the program).

EDIT: If you can't touch the other's main name from the code, change it after compiling. Exemple with gcc:

gcc -o other_main.o -c other_main.c
objcopy --prefix-symbols=foo_ other_main.o
gcc -o yourbinary other_main.o main.c

Then, call foo_main instead of main in your real main.

Upvotes: 8

Sohil Omer
Sohil Omer

Reputation: 1181

Its impossible to have two main's in one program

Upvotes: 0

Related Questions