我尝试编写一个非常短的脚本来打开 hdf5 文件,但它不起作用。
#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 );
}
我很确定我包含了 hdf5 库和包含的路径。 但尽管如此,我还是从链接器收到了错误消息:
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
有人可以帮忙吗? 谢谢!
对于那些使用 CMake 的人,这里有一个示例:
(最后一行解决了未定义引用问题)
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} )
我想给所有将来到达这个地方并遇到同样问题的人留下一条信息:
如果您选择使用带标志的
g++
来编译代码,而不是使用 hdf5 提供的 h5c++
脚本,请确保您使用的标志来自 h5c++ -show
而不是 h5cc -show
,因为后者用于直接 C 版本。
扩展 Bazel 用户的答案。 对我有帮助的是将原始评论中的标志添加到“linkopts”中。 那就是:
cc_binary(
name="h5-test",
srcs=[
"cpp/read_h5_test.cpp"
],
copts=[]
+ select(
{
"//conditions:default": [
"-I/usr/include/hdf5/serial/",
],
}
),
includes=[
"inc"
],
linkopts=[
"-lhdf5_serial",
"-lhdf5_cpp",
],
visibility=[
"//visibility:public"
],
)