当我为我的" makefile"创建runim.c文件的目标文件时 我收到此错误消息:
gcc:致命错误:没有输入文件
我不确定如何纠正这个问题。我提供了我的代码,但我认为问题不存在?我需要一个.h文件吗?我的fork()过程的实现是否正确?我将使用' testsim.c'文件和下面的代码一起获取命令行参数并根据要运行的进程数运行它
即。 runim 2< test.data
' runsim'是makefile中的可执行文件
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main (int argc, char *argv[])
{
FILE *file;
file = fopen("testsim.c", "w+");
int childpid;
int pr_count;
pr_count = 0;
char *line = NULL;
size_t len = 0;
ssize_t read;
int i;
int pr_limit;
read = getline(&line, &len, file);
int MAX_BUF;
MAX_BUF = read;
char buf[MAX_BUF];
pr_limit = atoi(argv[1]);
for (i = 0; i < pr_limit; i++)
{
childpid = fork();
if (childpid == 0) // Child Process Code
{
if (argc != 2)
{
fprintf(stderr, "Usage: %s processes\n", argv[0]);
return 1;
}
while(!feof(file))
{
if (pr_count == pr_limit)
{
waitpid(-1, &pr_count, WNOHANG);
pr_count--;
}
else
{
fgets(buf, MAX_BUF, file);
}
}
fclose(file);
waitpid(-1, &pr_count, WNOHANG);
exit(EXIT_SUCCESS);
fprintf(stderr, "i: %d process ID: %ld parent ID: %ld child ID: %ld\n", i, (long)getpid(), (long)getppid(),(long)childpid);
pr_count--;
}
if(childpid < 0)
{
perror("Error: waitpid() failed\n");//Fork Failed
return EXIT_FAILURE;
}
}
return 0;
}
Makefile包括:
CC = gcc
CFLAGS = -Wall -ansi
LIBS =
OBJ = runsim.o testsim.o
stats: $(OBJ)
$(CC) $(CFLAGS) -o runsim $(OBJ) $(LIBS)
runsim.o: runsim.c
$(CC) -c $(CFLAGS) runsim.c
testsim.o: testsim.c
$(CC) -c $(CFLAGS) testsim.c
.PHONY: clean
clean:
-rm *.o $(OBJ) runsim
当我尝试运行&#39; make&#39;时发生错误如下所示:
runsim.c: In function âmainâ:
runsim.c:22:2: warning: implicit declaration of function âgetlineâ [-Wimplicit-function-declaration]
read = getline(&line, &len, file);
^
runsim.c:33:22: error: expected expression before â/â token
if (childpid == 0) // Child Process Code
^
runsim.c:25:7: warning: unused variable âbufâ [-Wunused-variable]
char buf[MAX_BUF];
^
runsim.c:13:6: warning: variable âpr_countâ set but not used [-Wunused-but-set-variable]
int pr_count;
^
make: *** [runsim.o] Error 1