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

F

Francesco Moi

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

Joona I Palaste

Francesco Moi said:
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 ([email protected]) ------------- 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
 
M

Malcolm

Francesco Moi said:
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).
 
C

Christopher Benson-Manica

Malcolm said:
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?
 
M

Malcolm

Christopher Benson-Manica said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,365
Latest member
BurtonMeec

Latest Threads

Top