其他分享
首页 > 其他分享> > c – 有没有办法在gdb中打印犰狳矩阵?

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

作者:互联网

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

mat A = randu<mat>(5,5);

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

解决方法:

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

由于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;
}

现在您可以从gdb中轻松调用print_matrix:

(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

由于标签完成,您只需要实际输入print_matrix< arma :: Mat< double>的几个字符.取代.

标签:c,matrix,debugging,gdb,armadillo
来源: https://codeday.me/bug/20191007/1869587.html