如何在 clion 中使用像 wc 这样的 GNU 扩展作为 execvp 的参数?

How to use GNU extension like wc as execvp's argument in clion?

简单细分:

//temp.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>

int
main(int argc, char *argv[])
{
    char *myargs[3];
    myargs[0] = strdup("wc");
    myargs[1] = strdup("temp.c");
    myargs[2] = NULL;
    execvp(myargs[0], myargs);
}

在终端中:

$ gcc temp.c; ./a.out hello, I am child (pid:30232) 17 37 362 temp.c

好的,它工作正常。

在克隆中:

wc: temp.c: No such file or directory

那么,我如何在 clion 中启用像 wc 这样的 gnu 扩展?

CLion 在 CMake 项目设置中指定的单独构建目录中编译和 运行s 您的程序,通常它是项目根目录中的 cmake-build-debug 子目录。这意味着,当您 运行 您的程序时,工作目录中没有 temp.c 文件。

换句话说,您的项目布局很可能如下所示:

.
├── cmake-build-debug
│   ├── temp  ### <- your executable
│   └── ...
├── CMakeLists.txt
└── temp.c

尝试传递一个绝对路径到/path/to/temp.c,或者一个相对路径从工作目录找到它,也就是项目构建目录,即../temp.c.