简体   繁体   English

可执行路径作为 exec 的可变参数

[英]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.我有一个应用程序需要调用特定程序'mips64-unknown-linux-gcc'来链接脚本中的所有对象以及链接所需的所有参数。 I am writing an exec function to call the compiler passed by script along with it's args.我正在编写一个exec function 来调用脚本传递的编译器及其参数。 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.这可行,但mips64-unknown-linux-gccargv是来自脚本输入的变量。 I need execv first argument to be a variable which is compiler to be invoked.我需要execv第一个参数是要调用的编译器的变量。 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).我可以以某种方式(也许)通过getenv(”CC”) ) 检索它,但由于其他依赖项,我的要求是 exec 应在运行时接受编译器和 args(如下所示)。 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.这基本上与第一个参数的命令行 execpt 相同。 This makes things rather easy.这使事情变得相当容易。

Looking at argv you will find these string:查看argv你会发现这些字符串:

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.如果您想过滤一些选项并在您自己的程序中处理而不是盲目地将其传递给execvp ,您必须重建您自己的 arguments 数组,其中不包含这些选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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