5

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!

2
  • possible duplicate of Passing arguments to program run through gdb Commented Aug 17, 2013 at 7:49
  • Also compile your program with -g otherwise this is all for nothing Commented Aug 17, 2013 at 7:54

4 Answers 4

6

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
Sign up to request clarification or add additional context in comments.

7 Comments

he also has to compile with -g
@aaronman yes, to be able to use the debugger at all. But wasn't this question about the program arguments problem?
If he shows you how he's compiling it and you know it's wrong and you don't tell him, is your answer useful?
If we are at it, you should also add -Wall... ;-)
@LaszloPapp Cheers :D but I think that would really start going off topic. I can upvote your comment, though.
|
5

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.

4 Comments

A side note: If in case you have doubts in middle what args you set, use show args
@kingsmasher1 Exactly, I'm adding that to the answer.
after above procedure when I press s to start line by line debug it gives ` the program is not being run `
you need to run it by typing 'run'.
3

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

1 Comment

This will execute entire program and gives output. I want line by line debug!
3

Anther choice is provide argument after run

$gdb ./a
 run "Good nice"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.