升压序列化 - 导出多个cpp文件 [英] Boost Serialization - Exporting in multiple CPP files

查看:160
本文介绍了升压序列化 - 导出多个cpp文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几天我一直在用struggeling升压序列化问题:

The last days I have been struggeling with a Boost Serialization problem:

我试图序列化和在多个文件中的多个反序列化的导出类。为了保持它的通用I已创建的模板功能,如:

I am trying to serialize and deserialize multiple derived classes in multiple files. In order to keep it generic I have created template functions like:

template<typename T>
void
Helper::SaveToFile(T* data, std::string file)
{
    std::ofstream ofs(file.c_str());
    boost::archive::text_oarchive oa(ofs);
    oa << data;
}

有关的派生类的序列化工作,我需要使用升压宏BOOST_CLASS_EXPORT。但是,我不能把这个模板方法在CPP文件,并在标题中宏我得到这些恼人的重复init_guid的错误。

For serialization of derived classes to work, I need to use the Boost macro BOOST_CLASS_EXPORT. However, I cannot place this template method in a CPP file and with the macro in the header I get these annoying "duplicate init_guid" errors.

即使我选择不使用模板的方法,我仍然获得这些错误是由于这样的事实,我有多次在不同的文件不同的序列化方法,因此出口。

And even if I choose not to use a template method, I still get these errors due to the fact, that I have different serialize methods in different files and therefore exporting multiple times.

有没有人有任何HOWTO任何提示使其与模板方法,或者是如何工作的,以出口多个CPP文件中的类?

Does anyone have any tips on either howto make it work with template methods, or how to export classes in multiple CPP files?

我已经尝试拆分成BOOST_CLASS_EXPORT和BOOST_CLASS_EXPORT_KEY BOOST_CLASS_EXPORT_IMPLEMENT,仍然导致了同样的错误。另外,我不真的知道在哪里放置BOOST_CLASS_EXPORT_IMPLEMENT宏时,只有特定类的头文件。

I have already tried splitting BOOST_CLASS_EXPORT into BOOST_CLASS_EXPORT_KEY and BOOST_CLASS_EXPORT_IMPLEMENT, still leading to the same error. Also, I didnt really know where to put the BOOST_CLASS_EXPORT_IMPLEMENT macro when there is only a Header file for a specific class.

谢谢!

推荐答案

您是在正确的轨道上。

分成 BOOST_CLASS_EXPORT_KEY BOOST_CLASS_EXPORT_IMPLEMENT 的确是关键的解决方案。

Splitting into BOOST_CLASS_EXPORT_KEY and BOOST_CLASS_EXPORT_IMPLEMENT is indeed the key to the solution.

对于所有的C ++具有外部链接的符号,你

As with all C++ symbols with external linkage, you


  • 可以把声明的一些共享位置(如头文件)

  • 必须投入的定义的中的的翻译单元,以便只有一个连接器输入中包含一个定义。

  • can put declarations in some shared location (like the header file)
  • must put definitions in a single translation unit, so that only one linker input contains a definition.

在此情况下,只需包括 BOOST_CLASS_EXPORT_IMPLEMENT 在最多一个(静态)链接翻译单位(个人认为:CPP文件)。

In this case, simply include BOOST_CLASS_EXPORT_IMPLEMENT in at most one (statically) linked translation unit (think: cpp file).

请参阅背景:

  • How does the compilation/linking process work?
  • What is an undefined reference/unresolved external symbol error and how do I fix it?

这篇关于升压序列化 - 导出多个cpp文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆