Boost.Extension - 简单的继承示例 - 为什么我们在linux上看不到动物?

Rel*_*lla 2 c++ linux boost boost-extension

所以我尝试为Linux移植一些Boost.Extension示例.

这里描述样本.这是我的代码端口(类动物,动物原型,主应用程序,一般所有端口的想法在这里描述,以及一些当前的Linux进展(一些样本真的按需工作!)).当我在linux下编译这个样本时,它会编译,它会找到带有动物的库,但输出:

Animals not found!
Run Code Online (Sandbox Code Playgroud)

这只会发生if(factories.empty()).

我尝试将扩展示例移植到跨平台基础上 - 所以我在windows下尝试了相同的代码 - 就像一个魅力!找到所有动物和输出:

Creating an animal using factory:
Cougar factory Created an animal:
cougar Age: 2 Creating an animal using
factory: Leopard factory Created an
animal: leopard Age: 3 Creating an
animal using factory: Puma factory
Created an animal: puma Age: 4
Creating an animal using factory:
Wildcat factory Created an animal:
wildcat Age: 5
Run Code Online (Sandbox Code Playgroud)

那么......为什么它在linux上使用相同的代码呢?为什么它在Windows下运行良好?

更新:

那么如何使用premake构建这些东西:

  1. 从这里得到svn(只需要这个文件夹)
  2. 可以为您的平台预制或从源代码构建它并将其放入从svn下载的文件夹中
  3. 你应该编译和安装官方Boost(请阅读我们在目录中提供的ReadMe.txt文件)所以需要:
    • Boost C++库 (我们使用1.4.16版测试)
    • Boost-Extension(我们使用最新版本,我们将其作为boost'boost/extension/**'的一部分进行调整我们必须制作一些chandes(实际上只有一个)来提升扩展名,所以我们在Boost.Extension.Tutorial/libs/boost/extension/文件夹中提供它,所以当你下载svn时你得到了它,它只是标题)
    • Boost-Reflection(我们使用它是因为本教程,我们使用最新版本,我们将其作为boost'boost/reflection/**' *的一部分进行处理,为简单起见,我们建议将其放入Boost.Extension.Tutorial/libs/boost/reflection*)
  4. 现在当官方Boost在你的系统中时,标题只有Boost-reflection和Boost-extension在Boost.Extension.Tutorial/libs/boost文件夹中,premake4可执行文件在Boost.Extension.Tutorial/文件夹内,我们可以简单地调用Boost.Extension.Tutorial/ premake4-build-windows.batwindows来获取sln for Visual Studio或 Boost.Extension.Tutorial/ premake-build.sh获取makefile.
  5. 您可以在生成的项目文件夹中找到生成的解决方案/ makefile.
  6. 祝你好运!=)

更新2:

Windows和Linux的项目文件现在都在svn中,所以你可以通过premake来创建项目 - 只需要Boost,我们的svn和反射头只有lib.

seh*_*ehe 7

我在linux上调试了一些东西,好消息:

你正在遇到子弹没有.3来自Jeremy Pack的帖子:

RTTI并不总是在DLL边界上按预期运行.查看type_info类以了解我如何处理它.

我有一个小的解决方法补丁(下面)boost/extension/impl/typeinfo.hpp(但你需要与Boost扩展的维护者交谈,真的).这样做依赖于RTTI typeinfo的内置比较.

看看typeinfo.hpp,似乎Windows从未真正使用过typeinfo比较,所以我决定使用'strcmp'回退方法进行测试,并且瞧:

$ LD_LIBRARY_PATH=. ./Simple-Inheritance 
Creating an animal using factory: Cougar factory
Created an animal: cougar Age: 2
Creating an animal using factory: Leopard factory
Created an animal: leopard Age: 3
Creating an animal using factory: Puma factory
Created an animal: puma Age: 4
Creating an animal using factory: Wildcat factory
Created an animal: wildcat Age: 5
Run Code Online (Sandbox Code Playgroud)

特别是,我可以convertible_在type_map.hpp第68行显示类型查找失败;

  • 当从扩展dll本身调用此转换时,转换会愉快地使用RTTI查找匹配项.
  • 但是,当从测试应用程序完成' 相同 '.get()时(跨越DLL边界,即),RTTI是不同的,并且没有找到这样的匹配,并且命中了第74/75行:

.

73        if (it == instances_.end()) {
74          holder = new type_holder<StoredType>;
75          it = instances_.insert(std::make_pair(t, holder)).first;
76        }
Run Code Online (Sandbox Code Playgroud)

补丁

diff --git a/Boost.Extension.Tutorial/libs/boost/extension/impl/typeinfo.hpp b/Boost.Extension.Tutorial/libs/boost/extension/impl/typeinfo.hpp
index 843fed2..09fc353 100644
--- a/Boost.Extension.Tutorial/libs/boost/extension/impl/typeinfo.hpp
+++ b/Boost.Extension.Tutorial/libs/boost/extension/impl/typeinfo.hpp
@@ -50,7 +50,7 @@ struct type_info_handler<default_type_info, ClassType>

 // This list should be expanded to all platforms that successfully
 // compare type_info across shared library boundaries.
-#if defined(__APPLE__) || defined(__GNUC__) || \
+#if defined(__APPLE__) || \
     defined(BOOST_EXTENSION_FORCE_FAST_TYPEINFO)
 namespace boost {
 namespace extensions {
@@ -90,7 +90,7 @@ inline bool operator>(const default_type_info& first,
 }  // namespace extensions
 }  // namespace boost
 #else  // OTHER OS
-#include <string>
+#include <cstring>
 namespace boost { namespace extensions {
 inline bool operator<(const default_type_info& first,
                const default_type_info& second) {
Run Code Online (Sandbox Code Playgroud)