简体   繁体   中英

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

I am running into an error when compiling the following C++ code:

# 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;

}

Here is my error when compiling with 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)

When I replace the matrix multiplication '*' with '+', '%', etc. the code compiles without complaint.

Thanks in advance!

The error is a simple linker error which you can overcome by correctly building. Exactly what you need will depend on your system / OS (and is all documented) but on my Linux box this works:

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$ 

Here arma.cpp is the file containing your example. Linking with just the libarmadillo.so library is sufficient as it is linked to LAPACK and BLAS libraries. Other OSs may have different use patterns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM