简体   繁体   中英

c++ srand works when I run the program but doesn't work in gdb

c++ srand works when I run the program but doesn't work in gdb. I'm using Ubuntu logged into a terminal. Learning c++. My program runs as it is supposed to if I just execute it. But I really need to start learning to use the debugger. As I step thru the program the call to srand gives the following error.

12      srand(time(NULL));
(gdb) 
__srandom (x=1451559304) at random.c:210
210 random.c: No such file or directory.
(gdb) 
211 in random.c
(gdb) 
210 in random.c
(gdb) Quit

Here is the first part of the program, it is pretty simple.

1 #include <iostream>
2 #include <cstdlib>
3 #include <ctime>
4 #include <string>
5 
6 using namespace std;
7 
8 
9 int main()
10 {       
11         int array_size=0;
12         srand(time(NULL));
13         cout<<"\n\nWhat size of an array would you like?\n";
14         cout<<": ";
15         cin>>array_size;
16         int my_array[array_size];
17         for (int i=0;i<array_size;i++)
18         {
19                 my_array[i]=(rand()%100)+1;
20         }
21         for(int i=0;i<array_size;i++)
22         {
23                 cout<<"\n"<<my_array[i];
24 
25         }

When compiling I use this line.

g++ -g main.cpp -o a

And when I start the debugger it use this.

gdb a

I would appreciate any help. Thanks!

You are stepping into the standard library sources. Assuming you do HAVE the sources, you could set gdb up to find them with dir or set directories . But most people don't have the source for their C standard library - and unless you are actually trying to debug the standard library code itself, I'd suggest using the n (next) command instead of s (step) to step OVER the srand .

Note that the error you are getting is simply that gdb can't find the source file for srand . It will still step through the function eventually - but it will take some stepping, and without source code to view what it's doing, it's pretty pointless.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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