首页 新闻 赞助 找找看

Linux环境C编程错误:undefined reference to exec1

0
悬赏园豆:10 [已解决问题] 解决于 2014-10-24 15:41

程序中调用了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;
}

kwseeker的主页 kwseeker | 初学一级 | 园豆:195
提问于:2014-10-21 17:29
< >
分享
最佳答案
0

是execl不是exec1,书上印错了… 无语……

kwseeker | 初学一级 |园豆:195 | 2014-10-24 15:37
其他回答(1)
0

你少个exec1函数

收获园豆:10
CaiYongji | 园豆:1267 (小虾三级) | 2014-10-21 18:42

哪里少了?是标准库里面么?

支持(0) 反对(0) kwseeker | 园豆:195 (初学一级) | 2014-10-22 15:44
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册