4

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.

11
  • 13
    Why do you have two main functions? Commented Jul 20, 2015 at 14:44
  • 2
    stackoverflow.com/questions/22665187/… Answered here. Commented Jul 20, 2015 at 14:45
  • 3
    exec(), system()? Commented Jul 20, 2015 at 14:46
  • @Kay. Yeah, sort of. I have edited my question above. Not possible to change the name 'main' in foo.c. Commented Jul 20, 2015 at 14:55
  • 1
    Well, somewhere you should have a 'crt.c' source file that contains the code to set up the C runtime environment - heap etc, and then, somewhere near the end, jumps to or calls main(). If you edit the crt to call myMain() instead, you should be able to rebuild crt and your app so that myMain() gets called as the C entry point. You can then call the other main() whenever you wish. Commented Jul 20, 2015 at 23:36

7 Answers 7

8

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.

Sign up to request clarification or add additional context in comments.

3 Comments

No. It does not work. The true entry point is from my program. The 'main' from foo.c is another guy's program that I can not touch.
@zell Edited my answer.
Thank. I will try it today. Could you explain "objcopy --prefix-symbols=foo_ other_main.o"?
5

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).

Comments

4

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.

Comments

2

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.

Comments

2

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); 

Comments

0

Its impossible to have two main's in one program

Comments

0

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.

1 Comment

Please only add a new answer if it adds new information.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.