C++ 有没有办法在运行时设置 argv 和 argc 参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4355177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:13:22  来源:igfitidea点击:

Is there any way to set argv and argc parameters in runtime?

c++parametersparameter-passingargvargc

提问by reminder

I need to debug my program, the problem is that this program takes couple of parameters. How Can I debug program which takes a parameters ?? Can I somehow modify argc and argv parameters in runtime ??

我需要调试我的程序,问题是这个程序需要几个参数。如何调试带有参数的程序?我可以在运行时以某种方式修改 argc 和 argv 参数吗?

回答by Steve Townsend

The best way is not to modify the arguments at runtime but to debug an instance that has the arguments you want.

最好的方法不是在运行时修改参数,而是调试具有您想要的参数的实例。

For Windows you can do this in Visual Studio as follows:

对于 Windows,您可以在 Visual Studio 中执行此操作,如下所示:

  • Right click on project in Solution Explorer.
  • Set the arguments you want in Configuration Properties -> Debugging -> Command Arguments.
  • Hit F5 to start the program (after setting breakpoints where you want to stop).
  • 右键单击解决方案资源管理器中的项目。
  • 在配置属性 -> 调试 -> 命令参数中设置您想要的参数。
  • 按 F5 启动程序(在要停止的位置设置断点后)。

Alternatively start the program up as normal from the command line, and attach the debugger afterwards.

或者从命令行正常启动程序,然后附加调试器。

回答by Michael Burr

If you're invoking the debugger from the command line you can just add your command line arguments and the debugger will pass them on to your program.

如果您从命令行调用调试器,您只需添加命令行参数,调试器会将它们传递给您的程序。

If you're using an IDE, there should be a way to set the arguments that will be passed to your program (for example, in Visual Studio it's in the project properties under "Debugging/Command Arguments").

如果您使用的是 IDE,则应该有一种方法可以设置将传递给您的程序的参数(例如,在 Visual Studio 中,它位于“调试/命令参数”下的项目属性中)。

However, if I'm in a debug session and I want to debug using a variety of different command line arguments, I find it painful to have to edit the project properties continually. For that reason, I'll often make sure to have my argc/argvparsing in a function that takes parameters instead of acting on argc/argvdirectly, and have conditionally compiled in debugging code that passes in a hard coded command line string (I find it easier to modify the string in the source file than to edit the IDE's project properties) or I have the debugging code prompt for command line arguments.

但是,如果我在调试会话中并且想使用各种不同的命令行参数进行调试,我会发现必须不断编辑项目属性会很痛苦。出于这个原因,我会经常确保在一个接受参数而不是直接作用于/的函数中进行我的argc/argv解析,并且在传递硬编码命令行字符串的调试代码中有条件地编译(我发现它更容易修改源文件中的字符串而不是编辑 IDE 的项目属性)或者我有命令行参数的调试代码提示。argcargv

I have a handy little routine that'll parse a string into an argv-style array, then I can pass that to the routine that normally parses argc/argv.

我有一个方便的小例程,可以将字符串解析为一个argv样式数组,然后我可以将它传递给通常解析argc/的例程argv

So things might look something like:

所以事情可能看起来像:

int main( int argc, char** argv)
{
    if (debugging) {
        char** dbg_argv;
        int dbg_argc = argcargv( &dbg_argv, "dummyarg0 my debugging command --line");

        parse_options( dbg_argc, dbg_argv);
    }
    else {
        parse_options( argc, argv);
    }

    // etc...
}

It's not exactly pretty, but I find it more convenient than messing with project properties over and over.

它不是很漂亮,但我发现它比一遍又一遍地弄乱项目属性更方便。

回答by Martin York

If you are using GDB:

如果您使用 GDB:

 gdb ./a.exe
 > break main
 > run arg1 arg2 arg3 etc..