-2

i got no code or anything but an idea bumped up in my head . I have those 4 source codes a completed scripted files . i want to connect them with a 5th file with an if statement that if the user choices a number it's gonna excute an exact .c file . Exemple (not a real source code)

scanf("%d",&x)
if (x==1)
excute system1.c
if (x==2)
excute system2.c

etc etc any help please . i don't want to put the whole codes in the same file it's pain

Martin James
  • 24,453
  • 3
  • 36
  • 60
  • 2
    Normally you don't "execute a c file", instead you call a function that is defined in that c file. – interjay Oct 18 '20 at 09:52
  • 1
    C code isn't a script. You can execute programs. You can compile C code. You could, in theory, automate compiling and executing C code, but that's not recommended unless you have some very unusual requirements. – David Schwartz Oct 18 '20 at 09:54
  • I was commenting on your question, not the code. – interjay Oct 18 '20 at 09:55
  • I'm afraid you will have to include an example "more real". What are, in your example, system1.c and system2.c? Actual source files or executable files obtained after compiling them? In the first case whay you ask is some sort of dynamic inclusion that in C (that is not a scripting language) is not possible. In the latter case you could execute it through functions such as `system ()`, `popen`, `exec` and so on. – Roberto Caboni Oct 18 '20 at 10:01
  • You just had to google `system + C`.... Try [here](https://stackoverflow.com/a/5473235/11336762) – Roberto Caboni Oct 18 '20 at 10:06
  • What kind of help are you looking for? It is a function and it tells how to call it. I'll try a disperate last attempt: 1) you compile your program program1.c (it needs to have a main) - 2) you get an executable program1.exe - 3) you call `system (program1)`. Easy! – Roberto Caboni Oct 18 '20 at 11:19
  • Do not vandalize the question, even though it's your own. You may delete it if you wish, but wholesale replacing the original text and title as you have done is not acceptable, especially after you already have answers. – John Bollinger Oct 18 '20 at 15:45
  • I rolled it back AGAIN and flagged for repeated vandalism. – Martin James Oct 18 '20 at 16:55

2 Answers2

0

Here are some solutions, starting from the most common that covers the most use cases, to the most narrow and specialized:

  1. Compile all the files together into one program.

  2. Use a scripting language instead of C.

  3. Compile each program separately and use system to have one program execute another.

  4. Compile each program except the main one into a shared object file. Have the main program dlopen the appropriate shared object and call a "start" function in it.

  5. Have your main program launch the compiler to compile the applicable C code into a temporary execute, launch that executable (probably using system) and then delete it when it's done.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • @CarsonMarsin Compile each C file into a separate program. Then you can use `system` to have one program launch another. Another option is to use one of the `exec` family of functions to have one program be replaced by another. For example, if you compiled `foo.c` into `foo`, you can use `system("./foo");` to launch it. – David Schwartz Oct 18 '20 at 10:00
  • yes i understand david . But how to exactly use system . any guide tutorial ? i tried to search in google i couldn't find even a written guide – CarsonMarsin Oct 18 '20 at 10:00
  • You just pass the full path of the executable to the `system` function. [Here](https://www.geeksforgeeks.org/system-call-in-c/) is an example of a program that uses `system` to compile and run another program. – David Schwartz Oct 18 '20 at 10:02
  • Sorry. Is it not obvious what it's doing anyway? – David Schwartz Oct 18 '20 at 10:14
  • to be honest no , i want a detailed thing @david :p (first year in C that's why) – CarsonMarsin Oct 18 '20 at 10:15
0

Basic Example of working with the Multiple Files:

file1.c

void func1()
{
    printf("from func1\n");
}

file2.c

void func2()
{
    printf("from func2\n");
}

similarly write file3.c, file4.c ... fileN.c and func3() to func4() ... funcN()

As we have spread our entire program across multiple files, symbols in one .c file cannot be known to other .c file, as a result when we compile our program we will see some warning/errors.

Use of extern keyword
extern is a storage class

extern keyword is to declare the symbols which are defined in other source files.

Though we can directly use extern to declare symbols in other source files, the best practice is to create a header file(probably same name as that of source file with .h extension) and give the declarations there.

For all of our .c files we have created (except main.c) we need to create a .h file and add the function declaration there.

Ex: file1.h will have extern void func1();
and
file2.h will have extern void func2(); and so on so forth..

Note: functions by default will have extern storage class, generally ignored if called in same file where defined.

main.c

/* standard header files */
#include <stdio.h>
/* user defined header files */
#include "file1.h"
#include "file2.h"

/** add other header files here **/

int main()
{
    int ch;
    printf("enter choice (1-5):");
    scanf("%d", &ch);
    if(ch == 1)
        func1();
    else if(ch == 2)
        func2();
    else if(ch == 3)
        func3();
    else if(ch == 4)
        func4();
    else if(ch == 5)
        func5();
    else
        printf("invalied choice\n");
    return 0;
}

Compiling multiple files

gcc -Wall main.c file1.c file2.c file3.c file4.c file5.c -o test

./test
IrAM
  • 1,720
  • 5
  • 18
  • I'm not the downvoter, but though yiur solution makes sense (instead of compiling different programs) you forgot to mention the need to define a set of `fileN.h` files in which to declare the `funcN ()` prototypes. Or at least to suggest the use of the extern keyword – Roberto Caboni Oct 18 '20 at 11:24
  • @Roberto Caboni, Thanks anyway, I left that on purpose adding a **NOTE** indicating need to fix warnings, since there is no use of external variables here, dint suggest `extern` – IrAM Oct 18 '20 at 11:36
  • `extern` keyword tells the compiler that some variables issued by the current compilation unit are defined in a different compilation unit. But the same is true in case of external functions (in your case you `funcN ()`) – Roberto Caboni Oct 18 '20 at 12:55