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

c++, hdf5, linker-errors

Solution

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} )

Problem

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!

Original source