1

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!

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
Todd Young
  • 49
  • 7
  • Looks like the definition for the `*` operator is in the source file, and you're not linking the library in? – Steve Lorimer Jul 15 '16 at 15:47
  • Oops, I was unaware of linking in the library. However, now I am running into the following error: dyld: Library not loaded: /usr/local/opt/gcc/lib/gcc/6/libgfortran.3.dylib Referenced from: /usr/local/opt/arpack/libexec/lib/libarpack.2.dylib Reason: image not found Trace/BPT trap: 5 – Todd Young Jul 15 '16 at 16:12
  • Maybe this is of help? http://stackoverflow.com/questions/24993752/os-x-framework-library-not-loaded-image-not-found – Steve Lorimer Jul 15 '16 at 17:20
  • Looks like I am going to need a bit more digging; this is my first time using an external library, and I am not familiar with Xcode. The project I am working on is linking C++ into some R code for a machine learning task. – Todd Young Jul 15 '16 at 18:00
  • 1
    @ToddYoung - why not use just [RcppArmadillo](https://cran.r-project.org/web/packages/RcppArmadillo/index.html) where this is all taken care of for you? – mtall Jul 16 '16 at 07:22
  • As currently posed the question is all about Armadillo and has nothing to do with use from R (via Rcpp) so I am removing the Rcpp tag. – Dirk Eddelbuettel Jul 16 '16 at 11:11

1 Answers1

0

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.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Thanks for input, Dirk. I am a beginner and am still having some issues. I kept getting stuck so I decided to uninstall and reinstall using homebrew. after brew install I changed into the directory '/usr/local/Cellar/armadillo/7.200.2' and tried running CMake as directed in the readme.txt. I am now running into this error: directory does not appear to contain CMakeLists.txt. – Todd Young Jul 18 '16 at 17:35
  • @ToddYoung - after installing Armadillo using Homebrew, you don't need to install it again with CMake. – mtall Jul 19 '16 at 15:52