user123
user123

Reputation: 5407

Debug argument based C program with gdb

I have c++ program which I run by passing string with it.

g++ -o a main.cpp -lpthread

and execute it with ./a "Good nice"

But how I debug it with gdb? main.cpp calling functions from other files which are included in it.

gdb ./a "Good nice"

takes "--" as files and says no such file!

I want to debug line by line!

Upvotes: 5

Views: 425

Answers (4)

Use the --args option of gdb:

gdb --args ./a "Good nice"

Also add the -g option to your compiler call, because otherwise gdb won't be able to connect your executable with your source code:

g++ -g -o a main.cpp -lpthread

Upvotes: 6

billz
billz

Reputation: 45410

Anther choice is provide argument after run

$gdb ./a
 run "Good nice"

Upvotes: 3

Yu Hao
Yu Hao

Reputation: 122383

Use gdb without argument

gdb ./a

Then in gdb, before running the program

set args "Good nice"

And you can see what arguments you set, use

show args

See here for detail.

Upvotes: 5

elmov
elmov

Reputation: 286

gdb ./prog -> set args string -> run.

Upvotes: 3

Related Questions