-
October 19th, 2006, 10:43 PM
#1
Senior Member
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)
-
October 20th, 2006, 01:30 AM
#2
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
-
October 20th, 2006, 05:02 AM
#3
Senior Member
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)
-
October 20th, 2006, 08:43 AM
#4
execve has 3 parameters, but what I put in the example above is execv. It's basically a wrapper for execve.
ac
-
October 20th, 2006, 10:20 AM
#5
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
-
October 20th, 2006, 06:20 PM
#6
Senior Member
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)
-
November 15th, 2006, 08:18 AM
#7
Junior Member
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
-
Forum Rules
|
|