使用系统函数 运行 另一个 .cpp 文件
Using the system function to run another .cpp file
这个程序是一个'grader'程序,我只是要求用户输入一个txt文件的名称和一个处理txt文件并获取其信息的.cpp源文件。然后我编译源文件和 txt 文件,输出另一个文本文件。然后将这种新纺织品与预期的输出进行比较(我也得到了。)。
系统函数允许用户从 C 程序中 运行 UNIX 命令。当我尝试编译用户提供的源文件时
我收到一条错误消息
"_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
我正在编译的源文件由我的教授提供,其中有一个功能如下所示:
#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;
}
我写的代码如下:这段代码从用户那里获取信息,然后编译它。
#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;
}
如果您正在寻找解决方案,我已将其发布在答案中
您尝试编译的文件没有 main
函数,它是 C 程序的入口点。这意味着实际上不可能将该文件单独构建为可执行文件。
如果函数的名称应该是 notmain
,那么您将不得不编写另一个具有 main
函数并调用 notmain
的源文件。第二个 main
属于您的程序正在编译的可执行文件,而不属于您的程序。您将拥有三个源文件:
- 你的评分程序,处理编译。
一种有效的包装文件:
int main(int argc, char *argv[]) {
notmain(argc, argv);
}
- 最后是要评分的节目。
您还需要 extern
notmain
功能或提供 header 来共享它。然后,您的评分器程序将编译包装器 main
和要一起评分的源文件。
问题:你能运行两个具有2个主要功能的c程序吗?答案:是的。为此,您必须使用终端分别编译具有两个主要功能的程序。但是,如果他们彼此互动,恐怕我没有解决方案。现在,在这种特定情况下,我就是这样做的。我去了终端并写道。 在这种情况下,我 运行 一个程序 运行 另一个程序使用系统函数
gcc -c main.c (this compiles the main function).
然后我写了 gcc -o Myprogram main.o
这将创建一个名为 Myprogram 的可执行文件,您可以 运行 通过编写
./Myprogram
在这种情况下,我的主要方法是编译另一个源文件,因此我不需要在终端中也编译该程序。当我编译这个程序时,它在可执行文件和源文件所在的同一目录中创建了一个 output.txt 文件。
这个程序是一个'grader'程序,我只是要求用户输入一个txt文件的名称和一个处理txt文件并获取其信息的.cpp源文件。然后我编译源文件和 txt 文件,输出另一个文本文件。然后将这种新纺织品与预期的输出进行比较(我也得到了。)。
系统函数允许用户从 C 程序中 运行 UNIX 命令。当我尝试编译用户提供的源文件时
我收到一条错误消息
"_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
我正在编译的源文件由我的教授提供,其中有一个功能如下所示:
#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;
}
我写的代码如下:这段代码从用户那里获取信息,然后编译它。
#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;
}
如果您正在寻找解决方案,我已将其发布在答案中
您尝试编译的文件没有 main
函数,它是 C 程序的入口点。这意味着实际上不可能将该文件单独构建为可执行文件。
如果函数的名称应该是 notmain
,那么您将不得不编写另一个具有 main
函数并调用 notmain
的源文件。第二个 main
属于您的程序正在编译的可执行文件,而不属于您的程序。您将拥有三个源文件:
- 你的评分程序,处理编译。
一种有效的包装文件:
int main(int argc, char *argv[]) { notmain(argc, argv); }
- 最后是要评分的节目。
您还需要 extern
notmain
功能或提供 header 来共享它。然后,您的评分器程序将编译包装器 main
和要一起评分的源文件。
问题:你能运行两个具有2个主要功能的c程序吗?答案:是的。为此,您必须使用终端分别编译具有两个主要功能的程序。但是,如果他们彼此互动,恐怕我没有解决方案。现在,在这种特定情况下,我就是这样做的。我去了终端并写道。 在这种情况下,我 运行 一个程序 运行 另一个程序使用系统函数
gcc -c main.c (this compiles the main function).
然后我写了 gcc -o Myprogram main.o 这将创建一个名为 Myprogram 的可执行文件,您可以 运行 通过编写
./Myprogram
在这种情况下,我的主要方法是编译另一个源文件,因此我不需要在终端中也编译该程序。当我编译这个程序时,它在可执行文件和源文件所在的同一目录中创建了一个 output.txt 文件。