Li Chen
Li Chen

Reputation: 5270

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

Simple segment:

//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);
}

In terminal:

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

Ok, it works fine.

In clion:

wc: temp.c: No such file or directory

So, how can i enable gnu extension like wc in clion?

Upvotes: 0

Views: 153

Answers (1)

Eldar Abusalimov
Eldar Abusalimov

Reputation: 25523

CLion compiles and runs your program in a separate build directory specified in CMake project settings, usually it's a cmake-build-debug subdirectory in your project root. This means, there's no temp.c file in the working directory when you run your program.

In other words, your project layout most likely looks like:

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

Try passing an absolute path to /path/to/temp.c, or a relative path to find it from the working directory, which is the project build directory, that is, ../temp.c.

Upvotes: 1

Related Questions