c - 在主函数: undefined reference to exec中

标签 c gcc exec

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

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

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

$ cc foo.c
undefined reference to `exec'

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

最佳答案

您需要使用“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

关于c - 在主函数: undefined reference to exec中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25339617/

相关文章:

c - pthread_mutex_timedlock 和死锁

c - 将 char 指针强制转换为 int 指针是未定义的行为吗?

c - strtok坏了吗?或者只是棘手?

asynchronous - 从异步命令转到标准输出流

node.js - 从 golang 执行 nodejs 脚本的最佳方式是什么,该脚本返回一个字符串,然后将该字符串传回 golang 变量?

php - 如何在 PHP exec 中使用管道 ("| ")?

计算OpenSSL加密和解密的缓冲区大小?

c - subq $40 %rsp 与 AS 崩溃但 GCC 没有

c++ - 使用纯属性注释 const 成员函数

c++ - 传递引用阻碍 gcc 消除尾调用