Search code examples
ctext-filesprogram-entry-pointsrc

Using the system function to run another .cpp file


This program is a'grader' program, where I simply request the user to enter the name of a txt file and a .cpp source file which processes the txt file and gets its info. I then compile the source file along with the txt file, which outputs another text file. This new textile is then compared to the output expected(which I have been given as well.).

The system function allows users to run UNIX commands from a C program. When I am trying to compile the source file the user provides

I am getting an error saying that

"_main", referenced from: implicit entry/start for main executable.
clang: error: linker command failed with exit code 1 (use -v to see invocation)
sh: ./myProg: No such file or directory

The source file that I am compiling provided by my professor has one function which looks like this :

#include <stdio.h>
#include <stdlib.h>
#define  MAX_VALUES    3
#define  OUTPUT_LINES   5

int notmain(int argc, char **argv)
{
/*
 * argv is just the file name

 */
//printf(argv[1]);
int values[MAX_VALUES];
int i, j;


FILE *inputFile;
char name [20]="input.txt"; // I have included this piece of code to see if there is a correct output from the source file provided by the user. 
if ( (inputFile = fopen(name, "r") ) == NULL) {
     printf("Error opening input file.\n\n");
     exit(1);
}
for(i = 0; i < MAX_VALUES; i++)
    fscanf(inputFile, "%d", &values[i]);
for(i = 0; i < OUTPUT_LINES; i++){
   for (j=0; j < MAX_VALUES; j++)
      printf("%d ", values[j]*(i+1) + j);
   printf("\n");
}
return 0;
}

The code that I have written can be seen below: This code takes the information from the user and then compiles it.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM_LINES 5

int main(){
    char srcfile[200];
    char inpfile[200];
    char resultfile[200];

    printf("Please enter the name of the source file: \n");
    scanf("%s",srcfile);
    printf("Please enter the name of the input file: \n");
    scanf("%s",inpfile);
    printf("Please enter the name of the expected result file: \n");
    scanf("%s",resultfile);

    char test1 [100]="gcc -o myProg ";
    char test2 [100]="./myProg ";

    strcat(test2,inpfile);
    strcat(test2," > ");
    strcat(test2,resultfile);
    strcat(test1,srcfile);
    printf("%s\n",test1); //these are just tests 
    printf("%s",test2);  //these are just tests

    if (system(test1)) {
        printf("There is an error compiling the program  ");
    } 

    if (system(test2)!= 0) {
        printf("There is an error running the executable");
    } 

    return 0;
}

If you are looking for the solution I have posted it in the answers


Solution

  • The question: Can you run two c programs with 2 main functions? The answer: Yes . In order to do this you have to use the terminal to compile the program with the two main functions separately. However if they interact with one another Im afraid I don't have a solution to that Now in this specific case here is how I did it. I went to the terminal and wrote. In this case I run one program which runs another program using the system function

    gcc -c main.c (this compiles the main function). 
    

    Then after that i wrote gcc -o Myprogram main.o This will create an executable named Myprogram which you can run by writing

     ./Myprogram 
    

    In this case my main method is compiling another source file so I don't need to compile that program as well in the terminal. When i compiled this program it created an output.txt file in the same directory the executable and the source files are in.