std::chrono 动态库的谷物序列化问题

Cereal serialisation issue for std::chrono with dynamic library

我的谷物库 (http://uscilab.github.io/cereal/) 有问题。

我有一个共享库,我想使用 cereal 库序列化它的一个 classes。它有来自 std::chrono

的 time_point 成员

这是我在 Event.h

中对象的部分代码
    #include <cereal/types/chrono.hpp>
    #include <cereal/types/string.hpp>

    class Event
    {
    private:
         std::string m_Id;
         std::chrono::high_resolution_clock::time_point m_StartTime;
    public:
        template<class Archive> void serialize(Archive & archive)
        {
          archive(m_Id, m_StartTime);
        }

库编译没有问题。然后我想在我尝试序列化其中一个对象的可执行文件中使用我的库:

    #include "Event.h"
    #include <cereal/archives/json.hpp>
    cereal::JSONOutputArchive output(std::cout);
    output(API::Event());

此代码无法编译,它抱怨缺少 time_point 的序列化函数。如果我打算只序列化它编译的字符串。

构建错误输出:

[ 20%] Building CXX object CMakeFiles/plugin.dir/src/main.cpp.o
In file included from /cereal/include/cereal/types/memory.hpp:33:0,
             from main.cpp:7:
cereal/include/cereal/cereal.hpp: In instantiation of ‘ArchiveType& 
cereal::OutputArchive<ArchiveType, Flags>::processImpl(const T&) [with T = std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >; typename cereal::traits::detail::EnableIfHelper<(cereal::traits::has_invalid_output_versioning<T, ArchiveType>::value || ((! cereal::traits::is_output_serializable<T, ArchiveType>::value) && ((!(Flags & AllowEmptyClassElision)) || ((Flags & AllowEmptyClassElision) && (! std::is_empty<T>::value)))))>::type <anonymous> = (cereal::traits::detail::type)0; ArchiveType = cereal::JSONOutputArchive; unsigned int Flags = 0]’:
cereal/include/cereal/cereal.hpp:347:9:   required from ‘void cereal::OutputArchive<ArchiveType, Flags>::process(T&&) [with T = std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >&; ArchiveType = cereal::JSONOutputArchive; unsigned int Flags = 0]’
cereal/include/cereal/cereal.hpp:249:9:   required from ‘ArchiveType& cereal::OutputArchive<ArchiveType, Flags>::operator()(Types&& ...) [with Types = {std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >&}; ArchiveType = cereal::JSONOutputArchive; unsigned int Flags = 0]’
Event.h:66:16:   required from ‘void Event::serialize(Archive&) [with Archive = cereal::JSONOutputArchive]’
cereal/include/cereal/access.hpp:243:51:   required from ‘static decltype (t.serialize(ar)) cereal::access::member_serialize(Archive&, T&) [with Archive = cereal::JSONOutputArchive; T = Event; decltype (t.serialize(ar)) = void]’
cereal/include/cereal/cereal.hpp:400:33:   required from ‘ArchiveType& cereal::OutputArchive<ArchiveType, Flags>::processImpl(const T&) [with T = Event; typename cereal::traits::detail::EnableIfHelper<cereal::traits::has_member_serialize<T, ArchiveType>::value, (! cereal::traits::has_invalid_output_versioning<T, ArchiveType>::value), (cereal::traits::is_output_serializable<T, ArchiveType>::value && (cereal::traits::is_specialized_member_serialize<T, ArchiveType>::value || (! cereal::traits::is_specialized<T, ArchiveType>::value)))>::type <anonymous> = (cereal::traits::detail::type)0; ArchiveType = cereal::JSONOutputArchive; unsigned int Flags = 0]’
cereal/include/cereal/cereal.hpp:347:9:   required from ‘void cereal::OutputArchive<ArchiveType, Flags>::process(T&&) [with T = Event; ArchiveType = cereal::JSONOutputArchive; unsigned int Flags = 0]’
cereal/include/cereal/cereal.hpp:249:9:   required from ‘ArchiveType& cereal::OutputArchive<ArchiveType, Flags>::operator()(Types&& ...) [with Types = {Event}; ArchiveType = cereal::JSONOutputArchive; unsigned int Flags = 0]’
main.cpp:38:36:   required from here
cereal/include/cereal/cereal.hpp:462:9: error: static assertion failed: cereal could not find any output serialization functions for the provided type and archive combination. 

编辑:

我猜问题出在共享库中定义了序列化函数。

如果我只是编译一个测试 class(不在我的项目中只是为了测试)有一个 time_point 它会按预期工作。

好像是include的顺序问题。档案的包含必须在 types/chrono 的包含之前。所以正确的顺序是:

#include <cereal/archives/json.hpp>
#include "Event.h"
cereal::JSONOutputArchive output(std::cout);
output(API::Event());