I can't seem to get the c++ bindings for the HDF5 library to compile when using gcc.
The test program is the example found at: https://support.hdfgroup.org/ftp/HDF5/current/src/unpacked/c++/examples/h5tutr_crtdat.cpp
My cmake file reads as follows:
cmake_minimum_required(VERSION 3.1.0)
project(readhdf5 C CXX)
find_package(HDF5 COMPONENTS C CXX HL REQUIRED)
link_directories(${HDF5_INCLUDE_DIRS})
include_directories(${HDF5_INCLUDE_DIRS})
add_executable(readdata ../src/main.cpp)
target_link_libraries(readdata ${HDF5_LIBRARIES})
target_link_libraries(readdata ${HDF5_CXX_LIBRARIES})
Which I am using with the command:
cmake -DCMAKE_C_COMPILER=gcc-8 -DCMAKE_CXX_COMPILER=g++-8 ..
The error I recieve is:
me@comp:~/Downloads/hdf5-cmake-example-master/build$ make
[ 50%] Linking CXX executable readdata
Undefined symbols for architecture x86_64:
"H5::H5File::H5File(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int, H5::FileCreatPropList const&, H5::FileAccPropList const&)", referenced from:
_main in main.cpp.o
"H5::H5Location::createDataSet(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, H5::DataType const&, H5::DataSpace const&, H5::DSetCreatPropList const&) const", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make[2]: *** [readdata] Error 1
make[1]: *** [CMakeFiles/readdata.dir/all] Error 2
make: *** [all] Error 2
I'm having difficulty working out the cause of the error. If I do not specify the compiler it defaults to clang/clang++ which compiles fine. If I use only the C bindings then it compiles fine. If I specify the compilers as h5cc and h5c++ wrappers, then it compiles fine.
I'm not sure why the gcc/g++ compiler can't find the source code where clang/clang++ can.
I'm using macOS High Sierra. HDF5 was installed using Homebrew.
Thanks very much for your suggestions.
I understand what an undefined symbol is and how they're usually fixed. However the HDF5 library doesn't seem to follow the typical pattern of how an external library is linked. The question is not about how to fix undefined symbols in general but why they are not found only when using gcc in this case.
I suspect the answer requires specific knowledge of this library rather than general c++.