我在使用 gcc
和 execlp
时遇到问题。
下面是执行结果
root@ubuntu:~/sys/TP# ./sys
shoum.c: fatal error: no input files
compilation terminated.
done2
我的代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> /* for fork */
#include <sys/types.h> /* for pid_t */
#include <sys/wait.h> /* for wait */
int main(int argc,char** argv)
{
/*Spawn a child to run the program.*/
pid_t pid=fork();
if (pid==0)
{ /* child process */
execlp("gcc","shoum.c",NULL);
// execlp("ls","-liha",NULL);
printf("not working\n");
exit(127); /* only if execv fails */
}
else
{ /* pid!=0; parent process */
waitpid(pid,0,0); /* wait for child to exit */
printf("done2 \n");
}
return 0;
}
PS:当我使用其他命令如 ls
或 cat
时,execlp
可以工作,但它不适用于 gcc
。
最佳答案
你缺少一个参数,它应该是:
execlp("gcc", "gcc", "shoum.c", (char*)NULL);
第一个参数是要运行的程序,其余参数是程序的 argv[]
数组。您缺少 argv[0]
,它包含正在运行的程序的名称。所以 gcc
认为它正在以名称 shoum.c
运行并且没有文件名参数。
关于c - 将命令 gcc 与 execlp 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26561076/