У меня есть этот простой C++ код:не может печатать глобальные объекты в GDB
#include<bits/stdc++.h>
using namespace std;
vector<string> q;
int main()
{
q.push_back("test1");
q.push_back("test2");
cout<<q.front();
return 0;
}
Когда я использую gdb
для печати переменной q
я получаю следующую ошибку:
No symbol "q" in current context.
Я скомпилировать свою программу с помощью г ++ например:
g++ -g a.cpp
И вот мои команды gdb:
gdb a.out
GNU gdb (GDB) 7.12
Copyright (C) 2016 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 "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...done.
(gdb) b 6
Breakpoint 1 at 0x400c6f: file a.cpp, line 6.
(gdb) r
Starting program: /home/mohammad/Desktop/a.out
Breakpoint 1, main() at a.cpp:9
9 q.push_back("test");
(gdb) print q
No symbol "q" in current context.
Вероятно, Вы пропускаете файл символа в том же каталоге? –
Off topic: используйте '#include' и 'using namespace std;' с осторожностью и используйте их с особой осторожностью. Подробнее здесь [Почему «использование пространства имен std» считается плохой практикой?] (Http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) и [Почему я должен не #include ?] (http://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) –
user4581301
Возможно, потому что 'q' является глобальным. Взгляд на особенности 'print'. Назад в минуту – user4581301