ess-dmsc / h5cpp

C++ wrapper for the HDF5 C-library
https://ess-dmsc.github.io/h5cpp/
GNU Lesser General Public License v2.1
118 stars 19 forks source link

Require support to write and read 2D array with a raw pointer. #627

Closed ZL-Su closed 1 year ago

ZL-Su commented 1 year ago

I have tried to write and read data from an OpenCV container cv::Mat by "Using dataspaces with custom types". However it always reported the Link error of "unresolved external symbol" as:

LNK2019 unresolved external symbol "public: static class hdf5::datatype::Datatype cdecl hdf5::datatype::TypeTrait<class cv::Mat >::create(class cv::Mat const &)" (?create@?$TypeTrait@V?$Mat@M@cv@@@datatype@hdf5@@SA?AVDatatype@23@AEBV?$Mat@M@cv@@@Z) referenced in function "class hdf5::datatype::Datatype cdecl hdf5::datatype::create<class cv::Mat >(class cv::Mat const &)" (??$create@V?$Mat@M@cv@@@datatype@hdf5@@YA?AVDatatype@01@AEBV?$Mat@M@cv@@@Z)

I am here looking for ur response. Thx a lot.

Here is the code snippet:

namespace hdf5 {
namespace dataspace{
template<>
class TypeTrait<cv::Mat_<float>> {
public:
    using DataspaceType = Simple;
    using Cont_t = cv::Mat_<float>;

    static DataspaceType create(const Cont_t& m) {
        return DataspaceType({ size_t(m.rows), size_t(m.cols) });
    }

    template<class... _Args>
    const static DataspaceType& get(const Cont_t& C, _Args&...) {
        const static DataspaceType& cref = DataspaceType({ size_t(C.rows), size_t(C.cols) });
        return cref;
    }

    static void* ptr(Cont_t& C) {
        return reinterpret_cast<void*>(C.ptr<float>());
    }
    static const void* cptr(const Cont_t& C) {
        return reinterpret_cast<const void*>(C.ptr<float>());
    }
};
}};
yuelongyu commented 1 year ago

Hi @ZL-Su, According to the link error, the code is trying to use the hdf5::datatype::create<class cv::Mat >(class cv::Mat const &) which doesn't exist in your code, at least not in the snippet. You've only implemented the TypeTrait for cv::Mat in hdf5::dataspace.

You may have a look at this example. https://github.com/ess-dmsc/h5cpp/blob/master/src/h5cpp/contrib/stl/vector.hpp

ZL-Su commented 1 year ago

Hi@yuelongyu,

I have addressed this problem, thx.