g ++编译不识别'*'使用犰狳库进行矩阵乘法

g++ compiling not recognizing '*' for matrix multiplication using armadillo library

我在编译以下 C++ 代码时 运行 遇到错误:

# include <iostream>
# include <armadillo>

using namespace arma;
using namespace std;

int main() {
mat A;
mat B;
mat C;

// Populating the matrices with random numbers
A.randu(3,3);
B.randu(3,3);

// Matrix multiplication
C = A * B;

cout << "Mutliplying matrices A and B:" << endl;
cout << "A * B = " << C << endl;

return 0;

}

这是我用 g++ 编译时的错误:

Undefined symbols for architecture x86_64: "_wrapper_dgemm_", referenced from:

 void arma::blas::gemm<double>(char const*, char const*, int const*, > int const*, int const*, double const*, double const*, int const*, double > const*, int const*, double const*, double*, int const*) in   >armadillo_playground-aa3649.o

"_wrapper_dgemv_", referenced from:

 void arma::blas::gemv<double>(char const*, int const*, int const*, >double const*, double const*, int const*, double const*, int const*, >double const*, double*, int const*) in armadillo_playground-aa3649.o

ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see >invocation)

当我将矩阵乘法“*”替换为“+”、“%”等时,代码可以毫无怨言地编译。

提前致谢!

该错误是一个简单的 linker 错误,您可以通过正确构建来克服该错误。您究竟需要什么将取决于您的系统/OS(并且已全部记录在案)但是在我的 Linux 框中这是有效的:

edd@max:/tmp$ g++ -o arma arma.cpp -larmadillo 
edd@max:/tmp$ ./arma 
Mutliplying matrices A and B:
A * B =    1.0574   1.0356   1.5178
   1.1368   1.3434   1.4919
   0.7028   0.6516   1.0423

edd@max:/tmp$ 

此处 arma.cpp 是包含您的示例的文件。仅链接 libarmadillo.so 库就足够了,因为它链接到 LAPACK 和 BLAS 库。其他 OS 可能有不同的使用模式。