程序中调用了exec1();也包含了其头文件#include <unistd.h>
使用gcc编译时,产生错误:
Makefile文件内容:
ForkCreateP:fork.c
gcc fork.c -o ForkCreateP
错误信息:
/tmp/ccQzxmGo.o: In function `main':
fork.c:(.text+0x93): undefined reference to `exec1'
collect2: error: ld returned 1 exit status
make: *** [ForkCreateP] Error 1
网上相关的问题好象说是链接库和依赖的问题,但是我是初学者没看明白,不清楚怎么处理
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main(void)
{
pid_t pid;
if ((pid=vfork())<0) //对于程序来说父进程是什么?
{
printf("fork error!\n");
exit(1);
}
else if(pid==0) //子进程,遇见exec1时会运行一个新的进程代替子进程
{
printf("Child process PID: %d.\n",getpid() );
//注释:stdlib.h 在Linux和Windows里略有不同,比如setenv函数是用在Linux里的,而 //在Windows里则没有setenv函数,可用putenv来代替。
setenv("PS1","CHILD\\$",1); //系统环境变量PS1会在此进程中改为CHILD\\$,1 表示改变 //已存在的环境变量
printf("Process%4d:calling exec.\n",getpid());
if (exec1("/bin/ls","ls","-al",NULL)<0)
{
printf("Process %4d:exec1e error!\n",getpid());
exit(0);
}
printf("Process%4d: You should never see this because the child is already gone.\n", getpid());
printf("Process%4d: The child process is exiting.\n",pid);
}
else //父进程,必会运行
{
printf("Parent process PID:%4d.\n",getpid());
printf("Process%4d: The parent has fork process %d.\n",pid);
printf("Process%4d: The child has called exec or has exited.\n",getpid());
}
return 0;
}
是execl不是exec1,书上印错了… 无语……
你少个exec1函数
哪里少了?是标准库里面么?