简体   繁体   中英

Executable path as a variable parameter to exec

I've an application which needs to call a specific program 'mips64-unknown-linux-gcc' for linking all objects from a script with all required args for linking. I am writing an exec function to call the compiler passed by script along with it's args. For this I wrote the code:

//prog.c : gcc prog.c -o prog
int main(int argc, char *argv[]) {
   execvp("mips64-unknown-linux-gcc",argv);
}

This works, but the mips64-unknown-linux-gcc and argv are variables from script input. I need execv first argument to be a variable which is compiler to be invoked. I can somehow (maybe) retrieve it by getenv(”CC”) but due to other dependencies my requirement is that exec shall accept the compiler and args at runtime (something like below). Is there any way I can do this?

./prog mips64-unknown-linux-gcc --sysroot=<<...>> -O3 -Wl -L <<...>> -L <<...>> -I <<...>> -L <<...>> abcd.o a1.o b2.o -o prog

I described my problem at my best. Please ask if anything is not clear.

From your example command line it seems that you want to take the first argument from command line as your command to execute and everything else should be passed to that command.

That is basically the same command line execpt for the first argument. This makes things rather easy.

Looking at argv you will find these string:

char *argv[] = {"proc","mips64-unkown-linux-gcc", "--sysroot=<<...>>", ..., "-o", "prog", NULL};`

You can use that and call your command:

execvp(argv[1], argv+1);

Of course you should check whether you have at least one argument.

If you want do filter some options and handle in your own program instead of blindly passing it to execvp you must rebuild your own array of arguments where you do not include those options.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM