gdb用戶在這裏想到了使用模板和STL調試代碼的功能嗎?使用gdb調試帶模板和STL的C++代碼
你使用任何技巧使調試更簡單嗎?也許一些Python腳本?或者你是否滿意目前在gdb(版本6.x,還沒有嘗試過7.x)?
謝謝。
gdb用戶在這裏想到了使用模板和STL調試代碼的功能嗎?使用gdb調試帶模板和STL的C++代碼
你使用任何技巧使調試更簡單嗎?也許一些Python腳本?或者你是否滿意目前在gdb(版本6.x,還沒有嘗試過7.x)?
謝謝。
我假設你的意思是可視化的STL代碼更好的(而不是debug mode這給安全迭代器和額外的運行時檢查)。我不知道,如果你已經看過這些帖子:
用gdb
與7.0版本開始,GDB包括用Python寫漂亮的打印機支持。用於STL類的漂亮打印機使用版本4.5.0中的GCC分發。這些打印機的最新版本總是在libstdC++ svn倉庫中找到。爲了使這些打印機,簽出最新的打印機到本地目錄:
而且,嘗試使用的KDevelop/DDD如果可能的話 - 他們的幫助。
我最喜歡使用GDB的方式是emacs中的GDB模式。您可以獲得完整的視覺/源代碼級別的調試,線程窗口,堆棧窗口(等等)......試一試,您不會感到失望。
也就是說,GDB處理STL容器只是沒有特殊的附加功能,精細的調試......只要確保你與-g建設,沒有-ON(任何種類)...
DDD也很棒 - 我的最愛!
我不知道你是否被允許添加代碼,或者你只是在調試代碼,對不起。自從我寫了一個簡單的實用函數以來,我希望你覺得它有用。您可以輕鬆打印標準容器的內容。沒有平臺相關的代碼,使用的例子(測試驅動器實際上):我沒有寫了升壓容器的代碼,但
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <boost/array.hpp>
#include <boost/assign.hpp>
#include "streamer.hpp"
const std::size_t consoleWidth = 80;
std::ostream& newline_if_not_console(std::ostream& outputstream)
{
if(&outputstream != & std::cout)
{
outputstream << std::endl;
}
return outputstream;
}
void STL_test_ostream(std::ostream& out)
{
using namespace boost::assign;
using namespace streamer;
double iDoubleArray[] = {0.1, 1.2, 2.3, 3.4, 4.5}; // It could be of any type!
std::vector<int> iVec;
std::list<int> iList;
std::deque<int> iDeque;
std::stack<int> iStack;
std::queue<int> iQueue;
std::priority_queue<int> iPriorityQueue;
std::set<int> iSet;
std::map<int, std::string> iMap;
iVec += 0, 1, 2, 3, 4, 5;
iList += 0, 1, 2, 3, 4, 5;
iDeque += 0, 1, 2, 3, 4, 5;
iStack += 0, 1, 2, 3, 4, 5;
iQueue += 0, 1, 2, 3, 4, 5;
iPriorityQueue += 0, 1, 2, 3, 4, 5;
iSet += 0, 1, 2, 3, 4, 5;
insert(iMap)
( 1 , "one" )
( 2 , "two" )
( 3 , "three")
( 4 , "four" )
( 5 , "five" );
out << std::string(consoleWidth, '=') << newline_if_not_console
<< "STL Test..." << std::endl
<< std::string(consoleWidth, '=') << newline_if_not_console;
out << "Native Array = " << iDoubleArray << std::endl;
out << "vector = " << iVec << std::endl;
out << "list = " << iList << std::endl;
out << "deque = " << iDeque << std::endl;
out << "queue = " << iQueue << std::endl;
out << "stack = " << iStack << std::endl;
out << "priority_queue = " << iPriorityQueue << std::endl;
out << "set = " << iSet << std::endl;
out << "map = " << iMap << std::endl;
out << std::string(consoleWidth, '=') << std::endl;
}
void Boost_test_ostream(std::ostream& out)
{
out << std::string(consoleWidth, '=') << newline_if_not_console
<< "Boost Test..." << std::endl
<< std::string(consoleWidth, '=') << newline_if_not_console;
}
int main()
{
std::ofstream stl("STL_test_ostream.txt"),
boost("Boost_test_ostream.txt");
STL_test_ostream(std::cout);
Boost_test_ostream(std::cout);
STL_test_ostream(stl);
Boost_test_ostream(boost);
}
。但願,我會做一段時間:)
所有你所要做的,是包括這個文件[「streamer.hpp」]:
#ifndef DATASTRUCTRE_STREAMER
#define DATASTRUCTRE_STREAMER
#include <stack>
#include <queue>
#include <boost/array.hpp>
#include <functional>
#include <memory>
namespace streamer
{
// one-value data structure streaming function
template <class Container, class Stream>
Stream& printOneValueContainer(Stream& outputstream, const Container& container)
{
Container::const_iterator beg = container.begin();
outputstream << "[";
while(beg != container.end())
{
outputstream << " " << *beg++;
}
outputstream << " ]";
return outputstream;
}
// pair-value data structure streaming function
template <class Container, class Stream>
Stream& printPairValueContainer(Stream& outputstream, const Container& container)
{
Container::const_iterator beg = container.begin();
outputstream << "[";
while(beg != container.end())
{
outputstream << " " << "<" << beg->first << " , " << beg->second << ">";
beg++;
}
outputstream << " ]";
return outputstream;
}
/*
*************************************************************
C++ Standard Library
*************************************************************
*/
// Sequence Containers.
// vector, list, deque
template
< class Type
, template<class Type, class Allocator = std::allocator<Type> > class Container
, class Stream
>
Stream& operator<<(Stream& outputstream, const Container<Type>& container)
{
return printOneValueContainer(outputstream, container);
}
// Associative Containers.
// set, multiset
template
< class Key
, template<class KeyType, class Traits = std::less<KeyType>, class Allocator = std::allocator<KeyType> > class Container
, class Stream
>
Stream& operator<<(Stream& outputstream, const Container<Key>& container)
{
return printOneValueContainer(outputstream, container);
}
// map, multimap
template
< class Key, class Value
, template<class KeyType, class ValueType, class Traits = std::less<KeyType>, class Allocator = std::allocator<std::pair<const KeyType, ValueType> > > class Container
, class Stream
>
Stream& operator<<(Stream& outputstream, const Container<Key, Value>& container)
{
return printPairValueContainer(outputstream, container);
}
// Adapters.
// stack, queue
template < class Type, class Container >
const Container& container(const std::stack<Type, Container>& stack)
{
struct HackedStack : private std::stack<Type, Container>
{
static const Container& container(const std::stack<Type, Container>& stack)
{
return stack.*&HackedStack::c;
}
};
return HackedStack::container(stack);
}
template < class Type, class Container >
const Container& container(const std::queue<Type, Container>& queue)
{
struct HackedQueue : private std::queue<Type, Container>
{
static const Container& container(const std::queue<Type, Container>& queue)
{
return queue.*&HackedQueue::c;
}
};
return HackedQueue::container(queue);
}
template
< class Type
, template <class Type, class Container = std::deque<Type> > class Adapter
, class Stream
>
Stream& operator<<(Stream& outputstream, const Adapter<Type>& adapter)
{
return printOneValueContainer(outputstream, container(adapter));
}
// priority_queue
template < class Type, class Container, class Compare >
const Container& container(const std::priority_queue<Type, Container, Compare>& priorityQue)
{
struct HackedProiorityQueue : private std::priority_queue<Type, Container, Compare>
{
static const Container& container(const std::priority_queue<Type, Container, Compare>& priorityQue)
{
return priorityQue.*&HackedProiorityQueue::c;
}
};
return HackedProiorityQueue::container(priorityQue);
}
template < class Type, class Container, class Compare, class Stream >
Stream& operator<<(Stream& outputstream, const std::priority_queue<Type, Container, Compare>& adapter)
{
return printOneValueContainer(outputstream, container(adapter));
}
/*
*************************************************************
C++ Native Arrays
*************************************************************
*/
template <class Type, std::size_t size, class Stream>
Stream& operator<<(Stream& outputstream, Type (&array)[size])
{
outputstream << "[";
for(std::size_t i = 0; i < size; ++i)
{
outputstream << " " << array[i];
}
outputstream << " ]";
return outputstream;
}
/*
*************************************************************
Boost
*************************************************************
*/
}
#endif
感謝代碼示例和示例。目前我正在嘗試不使用任何外部代碼。理想情況下,我希望gdb可以自己做(漂亮的打印已經提到過)或者爲gdb製作一個腳本來幫助它做到這一點。從其他回覆中,我正在使用新版gdb在這方面做得更好。而且,順便說一句,我正在寫我自己的代碼。問候。 – user193272 2009-10-24 02:14:25
感謝,荷銀和dirkgently。我希望在幾天之內在我的Debian機器上運行GDB 7.0(運行Testing)。同時,我正試圖看看如何在.gdbinit中以正確的方式進行設置。 此致敬禮。 – user193272 2009-10-23 02:55:23