有时需要在通过gdb调试的时候,向程序传递命令行参数,假设如下程序:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
for(int i = 0; i < argc; i++)
{
cout << i << " :" << argv[i] << endl;
}
return 0;
}
可以使用以下几种方法:
1.运行gdb时通过--args传递参数
$ gdb --args main_args 1 2 3
GNU gdb (Ubuntu 12.1-0ubuntu1~22.04.2) 12.1
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "