f



Calling 'foo.c' or 'foo2.c' from my 'main_code.c'

Hello.

I don't know if following is possible.

I've got 'main_code.c':
............................
char * another_code;
another_code = "foo.c";
............................

I've got 'foo.c':
............................
#include <stdio.h>
int main() 
{
	printf ("Hello world from 'foo.c'!\n");
	return 0;
}
............................

I would like to call 'foo.c' main function from 'main_code.c'.

Is this possible?

Thank you very much and best regards.
0
4/18/2004 5:10:30 PM
comp.lang.c 30657 articles. 5 followers. spinoza1111 (3246) is leader. Post Follow

4 Replies
659 Views

Similar Articles

[PageSpeed] 18

Francesco Moi <francescomoi@europe.com> scribbled the following:
> Hello.

> I don't know if following is possible.

> I've got 'main_code.c':
> ...........................
> char * another_code;
> another_code = "foo.c";
> ...........................

> I've got 'foo.c':
> ...........................
> #include <stdio.h>
> int main() 
> {
> 	printf ("Hello world from 'foo.c'!\n");
> 	return 0;
> }
> ...........................

> I would like to call 'foo.c' main function from 'main_code.c'.

> Is this possible?

> Thank you very much and best regards.

If you link the object files compiled from main_code.c and foo.c
together, all you have to do is call main(). Otherwise, if they are
linked into separate executables, you essentially have to use
system() to call the other executable via the OS. This will, of
course, require using the name of the executable, not of its source.
If you want to call a specific function (not necessarily main()),
you can try to find a system-specific IPC (Inter-Process Communication)
feature from your OS, or implement a C interpreter.

-- 
/-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"You can pick your friends, you can pick your nose, but you can't pick your
relatives."
   - MAD Magazine
0
palaste (2323)
4/18/2004 5:18:10 PM
"Francesco Moi" <francescomoi@europe.com> wrote in message
>
> I don't know if following is possible.
>
> I've got 'main_code.c':
>
> I would like to call 'foo.c' main function from 'main_code.c'.
>
Not in standard ANSI C. Only one main() function is allowed which is the
program start point. You need to rename the "main" in foo.c as something
else (eg foomain).


0
Malcolm
4/18/2004 8:26:48 PM
Malcolm <malcolm@55bank.freeserve.co.uk> spoke thus:

> Not in standard ANSI C. Only one main() function is allowed which is the
> program start point. You need to rename the "main" in foo.c as something
> else (eg foomain).

Is it correct to say that your statements apply to hosted
implementations only?

-- 
Christopher Benson-Manica  | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org    | don't, I need to know.  Flames welcome.
0
ataru (1609)
4/19/2004 2:37:06 PM
"Christopher Benson-Manica" <ataru@nospam.cyberspace.org>
>
> Is it correct to say that your statements apply to hosted
> implementations only?
>
A non-hosted implementation may have a start point other than main(). It may
even allow main() as a normal function name, so you could link in foo.c with
a main(), once, when you try to link bar.c with it's main() you're back to
square one.


0
Malcolm
4/19/2004 9:11:14 PM
Reply: