3

Query This is the snippet where I call the main of another class by including its header file... As the title says, I want to invoke the main() of a program with parameters from another C program. Example: Suppose there are two programs: imain.c and another.c

I wish to call the main() of imain.c from the body of main in another.c and pass parameters..

Abhishek
  • 874
  • 2
  • 8
  • 23
  • 2
    [`man 3 exec`](http://pubs.opengroup.org/onlinepubs/009695399/functions/execv.html) –  Aug 15 '13 at 05:01
  • 2
    possible duplicate of [the second Google hit that someone couldn't be bothered to spend 2 minutes on searching for...](http://stackoverflow.com/questions/5460421/how-to-write-a-c-program-to-execute-another-program) –  Aug 15 '13 at 05:02
  • @H2CO3: Although related, that's a different question. –  Aug 15 '13 at 05:35
  • If you paste code into your question, please paste it *as text*, not as an image -- and be sure to format it as code (indent 4 spaces or highlight it and type control-K). – Keith Thompson Aug 15 '13 at 06:15
  • okay keith..wil keep in mind.. i pasted it from a virtual box..where bidirectional pasting was off... – Abhishek Aug 15 '13 at 06:16

1 Answers1

0

Create a header file with a function that exposes the functionality of the target main function. Then you can call that function by including that header file. A good example can be found http://www.tutorialspoint.com/cprogramming/c_header_files.htm.

Clocks
  • 411
  • 4
  • 8
  • Post your code so we can see what you are trying to do, and what errors you are getting – Clocks Aug 15 '13 at 05:10
  • i am so sorry..i cannot post the code ..company policies.. but i l post the errors right away.. – Abhishek Aug 15 '13 at 05:11
  • 2
    Regardless of how many headers you include, you can't have more than one function called `main` in an executable. – rici Aug 15 '13 at 05:14
  • I have pasted the code..thats the place where i call the main..which is included in one of the header files.. – Abhishek Aug 15 '13 at 05:20
  • 1
    Your code will not compile. Like I said, declare a new function that does exactly what your main function does in another header. Then you can replace that call to main with a call to the new function. And fyi, that call to main is not a valid way to call a C function. – Clocks Aug 15 '13 at 05:32
  • that will be cheating.. is there any straight forward method...as my main() program calls another 20 to 30 function.. it wont be feasible.. – Abhishek Aug 15 '13 at 05:38
  • 1
    That's as straightforward as it gets. It's not cheating. That's how C, (and a whole host of other languages) work. – Clocks Aug 15 '13 at 05:57
  • The answer is misleading. Do not use it. – zell Jul 20 '15 at 14:07