3

I'm quite new to C++ and armadillo, and I get stuck with a building error describe below. I'm trying to test the following simple code to save an armadillo matrix as hdf5 file:

#include <iostream>
#include <armadillo>

using namespace std;
using namespace arma;

int main()
{
    mat A = randu<mat>(240,320);
    A.save("A.hdf5",hdf5_binary);

    return 0;
}

When compiling, I get the following errors:

/usr/include/armadillo_bits/hdf5_misc.hpp:131: undefined reference in « arma_H5T_NATIVE_DOUBLE »
/usr/include/armadillo_bits/hdf5_misc.hpp:131: undefined reference in « arma_H5Tcopy »
obj/Debug/main.o: in function « bool arma::diskio::save_hdf5_binary<double>   (arma::Mat<double> const&, std::string const&) »:
/usr/include/armadillo_bits/diskio_meat.hpp:1299: undefined reference in « arma_H5Eset_auto »
/usr/include/armadillo_bits/diskio_meat.hpp:1308: undefined reference in « arma::H5check_version(unsigned int, unsigned int, unsigned int) »
/usr/include/armadillo_bits/diskio_meat.hpp:1308: undefined reference in « arma_H5Fcreate »
/usr/include/armadillo_bits/diskio_meat.hpp:1315: undefined reference in « arma_H5Screate_simple »
/usr/include/armadillo_bits/diskio_meat.hpp:1324: undefined reference in « arma_H5Dcreate »
/usr/include/armadillo_bits/diskio_meat.hpp:1330: undefined reference in « arma_H5Dwrite »
/usr/include/armadillo_bits/diskio_meat.hpp:1333: undefined reference in « arma_H5Dclose »
/usr/include/armadillo_bits/diskio_meat.hpp:1334: undefined reference in « arma_H5Tclose »
/usr/include/armadillo_bits/diskio_meat.hpp:1335: undefined reference in « arma_H5Sclose »
/usr/include/armadillo_bits/diskio_meat.hpp:1336: undefined reference in « arma_H5Fclose »

The compilation instruction is as follow:

g++ -Wall -fexceptions -O2 -g -larmadillo -lhdf5 -c main.cpp -o main

I'm working with CodeBlocks on a linux Fedora 20 system. I have the packaged HDF5-devel and could find the hdf5.h in /usr/include/ I also activated hdf5 in armadillo using the #define ARMA_USE_HDF5 in config.hpp. I use the latest version of armadillo (4.450) and gcc 4.8.3.

Did I missed a link ? To me, adding -larmadillo and -lhdf5 (as said in armadillo's users guide) should be enough. Any clue ? Thanks

jrgc
  • 45
  • 1
  • 6
  • possible duplicate of [g++ linking order dependency when linking c code to c++ code](http://stackoverflow.com/questions/3363398/g-linking-order-dependency-when-linking-c-code-to-c-code) – Benjamin Bannier Oct 10 '14 at 09:21
  • I voted this to close as a duplicate, the linked question should show you what to do and why (change the order of `-laramdillo` and `-lhdf5`). Either way I'd call this a problem in armadillo since it depends on libhdf5, but doesn't link against it. – Benjamin Bannier Oct 10 '14 at 09:25
  • I get your point and the post you link to is indeed interesting, but I suspected an order issue and checked this before posting. The problem was actually different (see answer below). – jrgc Oct 10 '14 at 15:18

1 Answers1

5

Due to various issues with differing versions of HDF5 libraries on Linux-based systems, the authors of Armadillo have disabled automatic detection of the HDF5 library. If you want to use HDF5 with Armadillo, there are two options:

1. Unpack the armadillo .tar.gz package, and edit the CMakeLists.txt file. Uncomment lines 231 to 238, starting with find_package(HDF5) (ie. remove the # characters). Once you have modified CMakeLists.txt, run the cmake based installation as described in the README.txt file.

OR

2. Install Armadillo normally (without modifying CMakeLists.txt), and then compile your programs using (all on one line):

g++ main.cpp -o main -O2 -DARMA_DONT_USE_WRAPPER -DARMA_USE_BLAS -DARMA_USE_LAPACK -DARMA_USE_HDF5 -lblas -llapack -lhdf5

Bonus points: if you have the high-speed OpenBLAS library installed and want Armadillo to use it instead of standard BLAS, change -lblas to -lopenblas

mtall
  • 3,574
  • 15
  • 23
  • 1
    Thanks a lot, I tried option 2 and it works fine. And I'll now us openblas as recommended. – jrgc Oct 10 '14 at 15:01
  • 2
    The CMakeList.txt file shows an argument for automatic detection that defaults to false: -D DETECT_HDF5=false So you can do: cmake -D DETECT_HDF5=true ../ – ben26941 May 10 '18 at 17:36