使用 gcc 时,我似乎无法获取要编译的 HDF5 库的 C++ 绑定(bind)。
测试程序的示例位于: https://support.hdfgroup.org/ftp/HDF5/current/src/unpacked/c++/examples/h5tutr_crtdat.cpp
我的cmake文件如下:
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})
我使用的命令是:
cmake -DCMAKE_C_COMPILER=gcc-8 -DCMAKE_CXX_COMPILER=g++-8 ..
我收到的错误是:
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
我很难找出错误的原因。 如果我不指定编译器,它默认为 clang/clang++ ,它编译得很好。 如果我只使用 C 绑定(bind),那么它编译得很好。 如果我将编译器指定为 h5cc 和 h5c++ 包装器,那么它可以正常编译。
我不知道为什么 gcc/g++ 编译器找不到 clang/clang++ 可以找到的源代码。
我使用的是 macOS High Sierra。 HDF5 是使用 Homebrew 安装的。
非常感谢您的建议。
我了解什么是 undefined symbol 以及它们通常如何修复。然而,HDF5 库似乎并不遵循外部库链接方式的典型模式。问题不在于如何修复一般 undefined symbol ,而在于为什么仅在这种情况下使用 gcc 时才找不到它们。
我怀疑答案需要该库的特定知识,而不是一般的 C++。
最佳答案
我在 HDF5 和 C++ 中遇到了非常相似的问题,最终与 this ABI issue 相关。 ,这也会影响 clang(当它无法通过诉诸较旧的 ABI 来修复时)。如果您的问题是由同一问题引起的,请继续阅读。
最终,我通过避免 HDF5 库接口(interface)中的 std::string
解决了这个问题。这意味着
- 不使用任何 HDF5 函数获取/返回
std::string
- 不使用任何使用
std::string
的 HDF5 结构
对于大多数情况,您可以简单地使用采用 const char*
并传递 string::c_str()
的 HDF5 例程版本。然而,还有两个更困难的情况。
HDF5 异常基于
std::string
,因此您不能使用它们,即catch
除了通过catch(...)
通过 HDF5 读取
std::string
。为此,我实际上重新实现了相应的例程(从 HDF5 源代码中窃取),以使用std::string
的正确 ABI 对其进行编译。这是:// // reading a std::string, see H5DataSet.cpp // std::string read_string(H5::DataSet const&data_set, H5::DataType const&mem_type, H5::DataSpace const&mem_space, H5::DataSpace const&file_space, H5::DSetMemXferPropList const&xfer_plist = H5::DSetMemXferPropList::DEFAULT) { const auto is_variable_len = H5Tis_variable_str(mem_type.getId()); if(is_variable_len<0) throw std::runtime_error("read_string: H5Tis_variable_str failed"); std::string strg; if(!is_variable_len) { const auto data_size = data_set.getInMemDataSize(); if(data_size>0) { std::unique_ptr<char[]> strg_C{new char[data_size+1]}; std::memset(strg_C.get(), 0, data_size+1); if(0>H5Dread(data_set.getId(), mem_type.getId(), mem_space.getId(), file_space.getId(), xfer_plist.getId(), strg_C.get())) throw std::runtime_error("read_string: H5Dread failed for fixed length string"); strg = strg_C.get(); } } else { char*strg_C; if(0>H5Dread(data_set.getId(), mem_type.getId(), mem_space.getId(), file_space.getId(), xfer_plist.getId(), &strg_C)) throw std::runtime_error("read_string: H5Dread failed for variable length string"); strg = strg_C; std::free(strg_C); } return strg; }
关于c++ - 使用 g++ 时 HDF5 的 undefined symbol ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51431246/