Varun Rao Varun Rao - 1 year ago 76
C Question

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

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");
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);
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");
printf("Please enter the name of the input file: \n");
printf("Please enter the name of the expected result file: \n");

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

strcat(test2," > ");
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;

Answer Source

Okay guys I have found the solution. So in the future if anyone has this question, to the more experienced users please show this post. 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

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


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.

Hope this helps for anyone interested in something like this in the future. And to those that are extremely rude to beginners using StackOverFlow I would suggest remembering when you were a beginner programmer and possibly give more helpful/positive constructed criticism which will motivate and egg others on to learn code.