Results 1 to 7 of 7

Thread: C program to invoke another C program's main() method?

  1. #1
    Senior Member
    Join Date
    Oct 2005
    Posts
    106

    C program to invoke another C program's main() method?

    OK, I've been searching the net for quite a while and I can't figure it out. I have a simple C program, let's call it a.c
    Code:
    #include <stdio.h>
    
    int main(int argc, char **argv) {
         printf("Hello World!\n");
         return 0;
    }
    and I have another C program (b.c) that needs to invoke a.c's main(argc, argv) method. How can this (if at all) be done?
    "The Texan turned out to be good-natured, generous and likeable. In three days no one could stand him." Catch 22 by Joseph Heller.

    Buddies? I have no buddies...


    Give the BSD daemon some love (proud FreeBSD user)

  2. #2
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    Why would you want to do that? The problem is how to distinguish between the two main methods. Are you not able to compile them as separate programs and then have b.c use one of the execve(...) functions to call a or something along those lines:

    Code:
    /* a.c: */
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
    	printf("Hello World!\n");
    	return 0;
    }
    
    /* b.c: */
    #include <stdio.h>
    #include <unistd.h>
    
    int main(int argc, char **argv)
    {
    	execv("./a", NULL);
    }
    I don't know if you can pass null as the second argument to execv, but if you look up the man page on it you should be able to figure it out.

    ac

  3. #3
    Senior Member
    Join Date
    Oct 2005
    Posts
    106
    Thanks, the reason I ask is because I'm working my way through Andy Tanenbaum's Minix book Operating Systems Design and Implementation and the toy shell code in there intrigued me quite a bit.

    But doesn't execve have three parameters? Or is that only for minix? I'll just look it up on the man pages, thanks!
    Last edited by Arkimedes; October 20th, 2006 at 05:05 AM.
    "The Texan turned out to be good-natured, generous and likeable. In three days no one could stand him." Catch 22 by Joseph Heller.

    Buddies? I have no buddies...


    Give the BSD daemon some love (proud FreeBSD user)

  4. #4
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    execve has 3 parameters, but what I put in the example above is execv. It's basically a wrapper for execve.

    ac

  5. #5
    Senior Member
    Join Date
    Jan 2002
    Posts
    1,207
    You could rename the main() method in a.c and then declare it in an external header file (a.h).

    Then include that header file in b.c and just call the renamed main method. When compiling the resulting program, link all of A and B's C files together.

    Slarty

  6. #6
    Senior Member
    Join Date
    Oct 2005
    Posts
    106

    Smile

    Quote Originally Posted by slarty
    You could rename the main() method in a.c and then declare it in an external header file (a.h).

    Then include that header file in b.c and just call the renamed main method. When compiling the resulting program, link all of A and B's C files together.

    Slarty
    I thought about that too, but the reason I am asking specifically for the main() function is because I am observing the code for a toy shell (I would post the code here, but I am unsure whether it is copyrighted; it is in the book Operating Systems: Design and Implementation, page 29 for the third edition to be precise ).

    [ASIDE]I am trying to get Minix to be a "real" operating system; an equivalent to Linux at least. I doubt I'll succeed soon (if at all) but everyone needs a hobby; a good friend of mine (who is a "real, professional" programmer) advised me to study the Minix book (OSDI) religiously.

    I figured the best way I could do that is to take extensive notes on the book, and do all the problems. (I must confess that I am learning quite a bit, maybe I'll write a tutorial on using minix 3!)[/ASIDE]
    "The Texan turned out to be good-natured, generous and likeable. In three days no one could stand him." Catch 22 by Joseph Heller.

    Buddies? I have no buddies...


    Give the BSD daemon some love (proud FreeBSD user)

  7. #7
    Junior Member
    Join Date
    Nov 2006
    Posts
    2
    Hi,
    execv()takes only two arguements.that'a why we include only one file.if u want to call more main methods header files are better.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •