【问题标题】:In function main: undefined reference to exec [closed]在 main 函数中:未定义对 exec 的引用 [关闭]
【发布时间】:2014-08-16 11:16:08
【问题描述】:

我遇到了this page, 我制作了这个文件

#include <unistd.h> 
int main(void) {
  exec("ls");
  return 0;
}

但是编译它会给我这个消息

$ cc foo.c
undefined reference to `exec'

那个页面是假的吗?过时了?这是怎么回事?

【问题讨论】:

标签: c gcc exec


【解决方案1】:

您需要使用“exec”函数系列之一。即以下之一:

int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg,..., char * const envp[]);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execvpe(const char *file, char *const argv[],

手册页exec 将告诉您它们的用法。也许

execl("/bin/sh", "-c", "ls");

或者使用opendirreaddirclosedir 编写ls

【讨论】:

    【解决方案2】:

    该页面不是伪造的。但是,link provided 引用的代码实际上是 Java,因为该站点引用了 ImageJ 程序,即“图像处理和分析”应用程序。见here

    【讨论】:

      【解决方案3】:

      你在找system()吗?

      #include <stdlib.h> 
      
      int main(void)
      {
        system("ls");
        return 0;
      }
      

      【讨论】:

      • system 和`exec`不一样。
      • @askmish,在 C、POSIX 中没有什么叫 exec ...
      • 是的,我知道没有什么叫 exec(),但是 system 调用不是 exec 函数家族所做的
      • @askmish,exec 系列中没有什么不需要第二个参数,所以我猜 OP 正在尝试执行命令,但也许我错了
      • 好吧,那是猜测。
      【解决方案4】:

      您看到的页面是关于Java 语言的,而不是C。如果要编译它,请使用 Java 编译器。如果您想要 C 中的类似功能,请使用其他答案中的建议之一。

      【讨论】:

        猜你喜欢
        • 2021-08-28
        • 2020-03-25
        • 1970-01-01
        • 2012-06-22
        • 2013-12-29
        • 2017-11-29
        • 1970-01-01
        • 1970-01-01
        • 2023-03-08
        相关资源
        最近更新 更多