有没有办法在gdb中打印犰狳矩阵?

Edd*_*ddy 7 c++ debugging gdb matrix armadillo

我正在使用gdb来调试我的c ++程序.我正在使用犰狳数值库来定义我的矩阵.我有一个如此定义的犰狳矩阵:

mat A = randu<mat>(5,5);
Run Code Online (Sandbox Code Playgroud)

使用gdb调试器时是否可以打印整个矩阵?

rer*_*erx 8

这个问题可能已经过时了,但对它的绊脚石让我找到了适合自己工作的解决方案.

由于Armadillo库的基于模板的特性,您需要提供一些自己的帮助:

#include <iostream>
#include <armadillo>

template<class Matrix>
void print_matrix(Matrix matrix) {
    matrix.print(std::cout);
}

//provide explicit instantiations of the template function for 
//every matrix type you use somewhere in your program.
template void print_matrix<arma::mat>(arma::mat matrix);
template void print_matrix<arma::cx_mat>(arma::cx_mat matrix);

int main() {
    arma::mat matrix = arma::randu(10,10);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在您可以轻松地print_matrix从内部拨打电话gdb:

(gdb) call print_matrix<arma::Mat<double> >(matrix) 
   0.8402   0.4774   0.0163   0.5129   0.5267   0.5260   0.2383   0.5316   0.6879   0.9565
   0.3944   0.6289   0.2429   0.8391   0.7699   0.0861   0.9706   0.0393   0.1660   0.5886
   0.7831   0.3648   0.1372   0.6126   0.4002   0.1922   0.9022   0.4376   0.4401   0.6573
   0.7984   0.5134   0.8042   0.2960   0.8915   0.6632   0.8509   0.9318   0.8801   0.8587
   0.9116   0.9522   0.1567   0.6376   0.2833   0.8902   0.2667   0.9308   0.8292   0.4396
   0.1976   0.9162   0.4009   0.5243   0.3525   0.3489   0.5398   0.7210   0.3303   0.9240
   0.3352   0.6357   0.1298   0.4936   0.8077   0.0642   0.3752   0.2843   0.2290   0.3984
   0.7682   0.7173   0.1088   0.9728   0.9190   0.0200   0.7602   0.7385   0.8934   0.8148
   0.2778   0.1416   0.9989   0.2925   0.0698   0.4577   0.5125   0.6400   0.3504   0.6842
   0.5540   0.6070   0.2183   0.7714   0.9493   0.0631   0.6677   0.3540   0.6867   0.9110
Run Code Online (Sandbox Code Playgroud)

由于选项卡完成,您只需要实际键入几个字符print_matrix<arma::Mat<double> >.


Max*_*kin 2

您可以在 中调用 C 函数gdb,因此您只需要一个打印对象的函数。例如:

(gdb) call printf("%.2f", 3.1428)
$7 = 4
(gdb) call fflush(stdout)
3.14$8 = 0
Run Code Online (Sandbox Code Playgroud)