mtl库在GCC编译器下的使用

最近一直在改造算法库,将其移植到Linux平台下。使用GCC编译器是发现MTL库中出现一大堆的问题。使用的MTL库下载地址为:http://osl.iu.edu/research/mtl/download.php3。在上面的网页中,需要下载下面三个压缩包,其中第一个是MTL库的源代码,第二个是在Visual Studio平台下编译的补丁包,第三个是gcc编译器的补丁包。

  1. http://osl.iu.edu/download/research/mtl//mtl-2.1.2-22.tar.gz
  2. http://osl.iu.edu/download/research/mtl//mtl-2.1.2-22ForVS2005.zip
  3. http://osl.iu.edu/download/research/mtl//mtl-2.1.2-22-gcc-4.02.patch.gz

测试代码如下:

#include <mtl/mtl.h>

int main(int argc, char* argv[])
{
	using namespace mtl;
	typedef matrix<float, rectangle<>, dense<>, row_major>::type Matrix;

	const Matrix::size_type MAX_ROW = 3, MAX_COL = 3;

	Matrix  A(MAX_ROW,MAX_COL),B(MAX_ROW,MAX_COL),C(MAX_ROW,MAX_COL);

	// fill Matrix A with the index syntax
	for (Matrix::size_type i=0; i<MAX_ROW; ++i)
	{
		for (Matrix::size_type j=0; j<MAX_COL; ++j)
		{
			A(i, j) = Matrix::value_type(rand() % 50);
		}
	}

	// fill Matrix B with the iterator syntax
	for (Matrix::iterator i=B.begin(); i!=B.end(); ++i)
	{
		for (Matrix::OneD::iterator j=(*i).begin(); j!=(*i).end(); ++j)
		{
			*j = Matrix::value_type(rand() % 50);
		}
	}

	mtl::print_all_matrix(A);
	mtl::print_all_matrix(B);

	// Matrix C = A + B
	add(A, C);
	add(B,C);
	mtl::print_all_matrix(C);

	// Matrix C = A * B^T,  B^T: transpose of B
	transpose(B);
	mtl::print_all_matrix(B);
	zero_matrix(C);        // because mult(A, B, C): C += A*B
	mult(A,B,C);
	mtl::print_all_matrix(C);
	return 0;
}

将第一个压缩包解压,并把第二个压缩包中的文件替换在第一个解压后的mtl目录中,然后引用mtl头文件即可。在使用VS2008编译正常,输出结果如下,截图见图1。

3x3
[
[41,17,34],
[0,19,24],
[28,8,12]
]
3x3
[
[14,5,45],
[31,27,11],
[41,45,42]
]
3x3
[
[55,22,79],
[31,46,35],
[69,53,54]
]
3x3
[
[14,31,41],
[5,27,45],
[45,11,42]
]
3x3
[
[2189,2104,3874],
[1175,777,1863],
[972,1216,2012]
]
请按任意键继续. . .

图1 VS2008运行结果

而在使用Code::Blocks中的gcc编译时,需要解压第一个压缩包,然后把第三个压缩包中的patch补丁更新到第一个压缩包解压的目录中去,至于怎么更新,在linux下很简单,Windows没找到工具,可以用记事本打开,找到更新的行然后替换(Windows下可以装个MSYS,然后使用Linux下的命令更新)。然后使用gcc编译上面的代码出现下面的错误信息。

-------------- Build: Debug in MTLTest (compiler: GNU GCC Compiler)---------------


mingw32-g++.exe -Wall -fexceptions  -g    -IF:\RsSrcDir\3rdPart\mtl-2.1.2-22_4gcc  -c F:\GCCTest\MTLTest\main.cpp -o obj\Debug\main.o
In file included from F:\RsSrcDir\3rdPart\mtl-2.1.2-22_4gcc/mtl/matrix.h:38:0,
                 from F:\RsSrcDir\3rdPart\mtl-2.1.2-22_4gcc/mtl/mtl.h:40,
                 from F:\GCCTest\MTLTest\main.cpp:1:
F:\RsSrcDir\3rdPart\mtl-2.1.2-22_4gcc/mtl/dense2D.h:186:95: error: wrong number of template arguments (1, should be 3)
F:\RsSrcDir\3rdPart\mtl-2.1.2-22_4gcc/mtl/dense2D.h:62:7: error: provided for 'template<class size_t, int MM, int NN> class mtl::rect_offset'
F:\RsSrcDir\3rdPart\mtl-2.1.2-22_4gcc/mtl/dense2D.h:186:95: error: ISO C++ forbids declaration of 'rect_offset' with no type
F:\RsSrcDir\3rdPart\mtl-2.1.2-22_4gcc/mtl/dense2D.h:186:95: error: 'mtl::rect_offset<size_t, MM, NN>::rect_offset' declared as an 'inline' variable
F:\RsSrcDir\3rdPart\mtl-2.1.2-22_4gcc/mtl/dense2D.h:186:95: error: 'int mtl::rect_offset<size_t, MM, NN>::rect_offset' is not a static member of 'class mtl::rect_offset<size_t, MM, NN>'
F:\RsSrcDir\3rdPart\mtl-2.1.2-22_4gcc/mtl/dense2D.h:186:95: error: template definition of non-template 'int mtl::rect_offset<size_t, MM, NN>::rect_offset'
F:\RsSrcDir\3rdPart\mtl-2.1.2-22_4gcc/mtl/dense2D.h:186:47: error: expected primary-expression before 'const'
Process terminated with status 1 (0 minutes, 0 seconds)
7 errors, 0 warnings (0 minutes, 0 seconds)
先看186行的错误,是rect_offset的一个构造函数出问题,模板不好改,而且gcc报出来的错误信息直接让人抓狂啊,各种晦涩。直接使用注释大法把这个函数注释掉。然后接着编译。然后你会发现编译过去了,运行结果如下,截图如图2所示:

3x3
[
[41,17,34],
[0,19,24],
[28,8,12]
]
3x3
[
[14,5,45],
[31,27,11],
[41,45,42]
]
3x3
[
[55,22,79],
[31,46,35],
[69,53,54]
]
3x3
[
[14,31,41],
[5,27,45],
[45,11,42]
]
3x3
[
[2189,2104,3874],
[1175,777,1863],
[972,1216,2012]
]

Process returned 0 (0x0)   execution time : 0.026 s
Press any key to continue.

图2 Code::Blocks使用gcc编译器运行结果

从上面的运行结果和截图,发现运行的结果是一样的。只能说明这样改是OK的,但是不排除有其他的问题,等遇到了再进行说明。

原文地址:https://www.cnblogs.com/xiaowangba/p/6313969.html