Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HDF5 - C++ - open a file to read the contents failed

I tried writing a really short script just to open an hdf5 file but it does not work.

#include <iostream>
#include "H5Cpp.h"

#ifndef H5_NO_NAMESPACE
    using namespace H5;
#endif

const H5std_string FILE_NAME( "testfile.h5" );

int main (void)

{

    H5File openFile( FILE_NAME, H5F_ACC_RDONLY );

}

I'm pretty sure that I included the hdf5 library and the path to the includes. But nevertheless I get the error message from the linker:

Invoking: GCC C++ Linker
g++ -L/usr/local/pub/lib64 -L/usr/local/pub/lib -L/lib64 -L/usr/lib64 -o "HDF5_CPP"  ./openfile.o   
./openfile.o: In function `main':
/athome/augs_ay/workspace/HDF5_CPP/Debug/../openfile.cpp:18: undefined reference to `H5check_version'
/athome/augs_ay/workspace/HDF5_CPP/Debug/../openfile.cpp:18: undefined reference to `H5::FileAccPropList::DEFAULT'
/athome/augs_ay/workspace/HDF5_CPP/Debug/../openfile.cpp:18: undefined reference to `H5::FileCreatPropList::DEFAULT'
/athome/augs_ay/workspace/HDF5_CPP/Debug/../openfile.cpp:18: undefined reference to `H5::H5File::H5File(std::string const&, unsigned int, H5::FileCreatPropList const&, H5::FileAccPropList const&)'
/athome/augs_ay/workspace/HDF5_CPP/Debug/../openfile.cpp:18: undefined reference to `H5::H5File::~H5File()'
collect2: error: ld returned 1 exit status
make: *** [HDF5_CPP] Error 1

can anyone help? Thank You!

like image 223
smaica Avatar asked Mar 26 '14 14:03

smaica


2 Answers

For those using CMake, here is an example:

(the undefined reference problem is solved in the last line)

find_package(HDF5 COMPONENTS C CXX HL REQUIRED)
link_directories( ${HDF5_LIBRARY_DIRS} )
include_directories( ${HDF5_INCLUDE_DIRS} )
add_executable( convert_to_hdf5 src/convert_to_hdf5.cpp )
target_link_libraries( convert_to_hdf5 ${HDF5_CXX_LIBRARIES} )
like image 81
Thomio Avatar answered Oct 02 '22 13:10

Thomio


I'd just like to leave a note to everyone else who reaches this place in the future, struggling with the same problem:

If you're choosing to use g++ with flags to compile your code instead of the hdf5-provided h5c++ script, make sure the flags you use are from h5c++ -show instead of h5cc -show, as the latter is for the straight C version.

like image 31
Dominik Stańczak Avatar answered Oct 02 '22 12:10

Dominik Stańczak