Boost.Extension - 简单继承示例 - 为什么我们在 Linux 上看不到动物?
所以我尝试为 Linux 移植一些 Boost.Extension 示例。
此处描述了该示例。这是我的代码端口(动物类,动物原型,主应用,常规所有端口想法均在此处描述,以及 这里有一些当前的 Linux 进展(一些示例确实可以根据需要工作!))。当我在 Linux 下编译这个示例时,它会找到包含动物的库,但输出:
Animals not found!
这只会发生 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
那么...为什么它在Linux上使用相同的代码表现如此?为什么它在Windows下运行得这么好?
更新:
那么如何使用 premake 构建这些东西:
- 你会得到 svn 来自此处(仅需要此文件夹)
- 您为您的平台获取 premake 或从源代码构建它并将其放入您从 svn 下载的文件夹中
- 您应该编译并安装官方 Boost(请阅读我们在目录中提供的 ReadMe.txt 文件),所以需要什么:
- Boost C++ 库(我们使用版本 1.4.16 进行测试)
- Boost-Extension(我们使用最新版本,我们将其作为 boost 'boost/extension/
的一部分**
' 我们必须制作一些chandes(实际上只有一个)来增强扩展,因此我们将其提供在Boost.Extension.Tutorial/libs/boost/extension/
文件夹中,以便当你下载 svn 时你就得到了它,它只是标题) - Boost-Reflection(我们使用它因为本教程< /a> ,我们使用 最新版本 ,我们将其作为一部分boost 'boost/reflection/
**
' *为了简单起见,我们建议将其放入Boost.Extension.Tutorial/libs/boost/reflection
* )
- 现在,当您的系统中有官方 Boost 时,只有 Boost-reflection 和 Boost-extension 头文件位于
Boost.Extension.Tutorial/libs/boost
文件夹中,premake4 可执行文件位于 < code>Boost.Extension.Tutorial/ 文件夹中,我们可以简单地在 Windows 上调用Boost.Extension.Tutorial/ premake4-build-windows.bat
来获取 Visual Studio 或Boost 的 sln .Extension.Tutorial/ premake-build.sh
获取 makefile。 - 您可以在生成的项目文件夹中找到生成的解决方案/makefile。
- 祝你好运!=)
更新 2:
项目文件 适用于 Windows 和 Linux 的版本现在位于 svn 中,因此您可以使用 premake 来创建项目 - 只需拥有 Boost、我们的 svn 和反射标头专用库即可。
So I try to port some Boost.Extension samples for linux.
The sample is described here. Here is my code port (classes with animals, animal prototype, main app, general all port idea is described here, and some current linux progress here (some samples really work as needed!)). When I compile this sample under linux it compiles, it finds library with animals but outputs:
Animals not found!
Which shall happen only if(factories.empty())
.
I try to port Extension samples onto crossplatform base - so I have tried same code under windows - works like a charm! finds all animals and outputs:
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
So... Why it behaves so on linux with same code? Why it works so well under Windows?
Update:
So how to build this stuff with premake:
- You get svn from here (only this folder is required)
- You get premake for your platform or build it from source and put it into folder you downloaded from svn
- You should have official Boost compiled and installed (please read ReadMe.txt file we provide in directory) so what is needed:
- Boost C++ library's (we tested with version 1.4.16)
- Boost-Extension ( we use latest revision , we adress it as part of boost 'boost/extension/
**
' We had to make some chandes (actually only one) to boost extension so we provide it insideBoost.Extension.Tutorial/libs/boost/extension/
folder so when you downloaded svn you got it, it is header only ) - Boost-Reflection ( we use it because of this tutorial , we use latest revision , we adress it as part of boost 'boost/reflection/
**
' *and for simplness we recommend just to put it intoBoost.Extension.Tutorial/libs/boost/reflection
* )
- Now when official Boost is in your system, header only Boost-reflection and Boost-extension are in
Boost.Extension.Tutorial/libs/boost
folder, premake4 executable is insideBoost.Extension.Tutorial/
folder we can simply callBoost.Extension.Tutorial/ premake4-build-windows.bat
on windows to get sln for Visual Studio orBoost.Extension.Tutorial/ premake-build.sh
to get makefiles. - You can find generated solution/makefiles inside generated projects folder.
- Have good luck!=)
Update 2:
Project files for Windows and Linux are now in svn so you can get aroung project creation with premake - just have Boost, our svn, and reflection headers only lib.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 Linux 上调试了一些东西,好消息:
您遇到了 项目符号号。 3 来自 Jeremy Pack 的帖子:
我有一个针对
boost/extension/impl/typeinfo.hpp
的小解决方法补丁(如下)(但您确实需要与 Boost Extension 的维护者交谈)。其作用不依赖于 RTTI 类型信息的内置比较。查看 typeinfo.hpp,似乎 Windows 从未真正使用过 typeinfo 比较,因此我决定使用“strcmp”后备方法进行测试,瞧:
特别是,我可以显示从
convertible_
convertible_ 进行的类型查找code> 在 type_map.hpp 第 68 行失败;。
修补
I debugged things on linux, good news:
You are running into bullet no. 3 from Jeremy Pack's post:
I have a tiny workaround patch (below) to
boost/extension/impl/typeinfo.hpp
(but you need to talk to the maintainer of Boost Extension, really). What this does is not rely on builtin comparison for RTTI typeinfo's.Looking at typeinfo.hpp, it seems that Windows never actually uses the typeinfo comparison, so I decided to test with the 'strcmp' fallback method, and voila:
In particular, I can show that the type lookup from
convertible_
fails at type_map.hpp, line 68;.
Patch
默认情况下,Linux 上的 GCC 具有比 Windows 上的 MSVC 更严格的链接器优化设置。这会导致一些工厂模式,其中类将自身注册为可用,但看起来已损坏,这仅仅是因为链接器优化了类。我没有看你的代码 - 但从描述来看这可能是问题所在。
一个相关的问题,以及如何避免删除未引用的类的答案:如何强制 gcc 从库链接未引用的静态 C++ 对象
GCC on Linux by default has stricter linker optimization settings that MSVC on Windows. This leads to some factory patterns where classes register themselves as available appearing broken, simply because the linker optimizes away the classes. I didn't look at your code - but from the description it could be the problem.
A related question, with an answer on how to avoid the unreferenced classes being dropped: How to force gcc to link unreferenced, static C++ objects from a library