0

I'm looking for an efficient (fast and secure) method to communicate multiple scripts (and their associated main function ()) to each other. A bit like the principle of the G-WAN project which uses a launcher (./gwan) to read / load / compile different .c files which each contain (or not) a main() function.

Ideally, my launcher should be able to execute the main () functions of other scripts while sharing information through their argv variables.

But as you know, gcc -Wall script1.c script2.c script3.c -o test return me an error of multiple definition of function main(), and gcc -Wl,--allow-multiple-definition -Wall script1.c script2.c script3.c -o test interprets only the first script1.c main() function.

Maybe the solution would be to have a first script (script1.c) which compiles the other scripts (script2.c and script3.c) via a shared variable?

Thanks for your help and sorry for my limited english.

script1.c:

int main(int argc, char *argv[]){
  ...
  int i = main(argc, argv); // main for script2.c
  if(i == 0)
    main(argc, argv); // main for script3.c
  ...
  return(0);
}

script2.c:

int main(int argc, char *argv[]){
  ...
  return(0);
}

script3.c:

int main(int argc, char *argv[]){
  ...
  return(0);
}
John S
  • 231
  • 3
  • 11
  • 2
    You need to compile each program separately and either use `system` or `fork` and `exec` to start a new process. – dbush Oct 17 '21 at 15:53
  • 2
    BTW there are no "scripts" in C. – Jabberwocky Oct 17 '21 at 16:03
  • good idea dbush, but my problem with this solution would be (for example) to create a variable in script1.c, which would be passed and modify in script2.c, to then be used (variable modified) in script1.c ... do you have solution ? (shared memory ?) – John S Oct 18 '21 at 23:00

1 Answers1

0

You can't have multiple main function in c, it's simply not allowed, what you can do is have your main function in your first .c file, and whatever you want in your others .c files.

Then you can write :

#include "script2.c"
#include "script3.c"

on top of your script1.c file.

If you want to compute values inside a file and "send" it to main file, you can do it through functions, defined in your other files, since you include it !

Notice that there is no scripts in C !

Indeblion
  • 15
  • 7