armadillo
是一个线性代数 C++库,封装了blas
和lapack
,提供更直观的接口。
1、安装依赖项
sudo apt-get install cmake libopenblas-dev liblapack-dev libarpack2-dev libsuperlu-dev -y
2、下载
wget http://sourceforge.net/projects/arma/files/armadillo-12.8.2.tar.xz
最新的版本可以在 http://arma.sourceforge.net/download.html 找到。
3、安装
tar -Jxvf armadillo-12.8.2.tar.xz
cd armadillo-12.8.2
cmake . -DCMAKE_INSTALL_PREFIX=/opt
make
make install
4、测试
cd example1
g++ -o example1 example1.cpp -O2 -larmadillo
./example1
5、链接错误
测试程序出现下面的编译错误:
/tmp/ccstw4HV.o: In function `double arma::auxlib::det_lapack<double>(arma::Mat<double> const&, bool)':
example1.cpp:(.text._ZN4arma6auxlib10det_lapackIdEET_RKNS_3MatIS2_EEb[_ZN4arma6auxlib10det_lapackIdEET_RKNS_3MatIS2_EEb]+0xba): undefined reference to `wrapper_dgetrf_'
/tmp/ccstw4HV.o: In function `std::ostream& arma::operator<< <double, arma::Op<arma::Mat<double>, arma::op_inv> >(std::ostream&, arma::Base<double, arma::Op<arma::Mat<double>, arma::op_inv> > const&)':
example1.cpp:(.text._ZN4armalsIdNS_2OpINS_3MatIdEENS_6op_invEEEEERSoS6_RKNS_4BaseIT_T0_EE[_ZN4armalsIdNS_2OpINS_3MatIdEENS_6op_invEEEEERSoS6_RKNS_4BaseIT_T0_EE]+0x166): undefined reference to `wrapper_dgetrf_'
example1.cpp:(.text._ZN4armalsIdNS_2OpINS_3MatIdEENS_6op_invEEEEERSoS6_RKNS_4BaseIT_T0_EE[_ZN4armalsIdNS_2OpINS_3MatIdEENS_6op_invEEEEERSoS6_RKNS_4BaseIT_T0_EE]+0x25e): undefined reference to `wrapper_dgetri_'
example1.cpp:(.text._ZN4armalsIdNS_2OpINS_3MatIdEENS_6op_invEEEEERSoS6_RKNS_4BaseIT_T0_EE[_ZN4armalsIdNS_2OpINS_3MatIdEENS_6op_invEEEEERSoS6_RKNS_4BaseIT_T0_EE]+0x404): undefined reference to `wrapper_dgetri_'
collect2: error: ld returned 1 exit status
没找到具体原因。但把/usr/lib
和/usr/include
下面的 armadillo 删掉可以解决,这可能是因为系统内存在老版本导致的:
sodu rm /usr/lib/libarmadillo*
rm /usr/include/armadillo* -r
删除后运行程序会出现运行时链接错误:
./example1: error while loading shared libraries: libarmadillo.so.7: cannot open shared object file: No such file or directory
这是因为/usr/local/lib
没有加到LD_LIBRARY_PATH
。我们也可以手工把 local 下面的 so 复制到/usr/lib
下:
sodu cp /usr/local/lib/libarmadillo* /usr/lib/
Q. E. D.