c - 将命令 gcc 与 execlp 一起使用

标签 c linux gcc fork

我在使用 gccexeclp 时遇到问题。
下面是执行结果

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:当我使用其他命令如 lscat 时,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/

相关文章:

c - GCC 迭代函数优化

python - c/Python 等价于这样的循环?

c - 如何向文本或 C 字符串中的字母添加下标字符?

c - Scanf 更多值 C

c - 取消引用 NULL 指针 'head' ,'tail

linux - 列出按大小降序排列的所有目录

sql - 用于自定义/预定义 SQL 查询的 Web GUI

C: gcc 将 signed char 隐式转换为 unsigned char,反之亦然?

c - 使用 gcc 的中间 GIMPLE 格式

linux - 使用变量操作 bash 脚本输出