提升文本序列化版本(15 vs 18)

boost text serialization version (15 vs 18)

我有一个自定义 class,其中只包含一个 std::map<std::string, int>。 我正在尝试使用 boost::serialization.

对其进行序列化

我用数据 {"foor":13} 创建了一个对象(这是我的自定义对象中的地图)。 Boost 将其序列化为

22 serialization::archive 15 22 serialization::archive 15 0 0 0 0 1 0 0 0 4 foor 13

到目前为止,一切顺利 (?)。

现在,我采用相同的源代码(几乎没有改编),我编译为 wasm 而不是 x86。 同一个对象现在被序列化为

22 serialization::archive 18 0 0 0 0 1 0 0 0 4 foor 13

因此,出于某些原因,在一种情况下,Boost 选择使用版本 15 的存档,而在另一种情况下,它选择版本 18。 (显然,x86 生成的反序列化在 wasm 程序中不起作用)

我怎样才能 "force" 提升使用版本 15 的存档?

第一个序列化样本看起来是 全部无效。

其次,让我们检查一下 15 和或 18 是 存档版本号:

#include <boost/archive/text_oarchive.hpp>
#include <iostream>
int main() {
    unsigned v;
    {
        boost::archive::text_oarchive oa(std::cout);
        v = oa.get_library_version();
    }

    std::cout << "# get_library_version() -> " << v << "\n";
}

打印(see live

22 serialization::archive 17
# get_library_version() -> 17

所以,是的,看起来很准确

由于它们在存档实现中是硬编码的,因此您必须在两侧使用相应的 Boost 库版本。

相关文档片段:

get_library_version()

Returns an unsigned integer containing the current version number of the serialization library. This number will be incremented each time the library is altered in such a way that serialization could be altered for some type. For example, suppose the type used for a count of collection members is changed. The code that loads collections might be conditioned on the library version to make sure that libraries created by previous versions of the library can still be read.

棘手的一点是为 wasm 和 x86 构建 Boost::serialization

克隆boost,进入克隆目录

wasm

我得到了这一行(网络上许多研究的拼凑;我什至不确定每个参数的 meaning/usefulness)

./b2 toolset=emscripten link=static variant=release threading=single --prefix=build_wasm --build-dir=build_wasm runtime-like=static serialization

x86

./bootstrap.sh --prefix=build_x86
./b2

下一个

此时,我们有两个分开的目录build_wasmbuild_x86到link到

至此,我不记得我是如何设法获取文件的 libboost_serialization.bc

cmake

在我的 CMakeLists.txt 中,关键行是:

set(BoostWasm_DIR "/absolute/path/to/build_wasm")
set(BoostWasm_emm "${BoostWasm_DIR}/boost/bin.v2/libs/serialization/build/emscripten-1.39.13/release/link-static/visibility-hidden")

include_directories(${BoostWasm_DIR}/include)

file(GLOB boost_js "${BoostWasm_emm}/libboost_serialization.bc")
target_link_libraries( write_normalized ${boost_js} )

由于这是一个与工作相关的项目,我没有 link 完整的工作示例。

建设

emcmake cmake .
emmake make