最近在学习Ogre,编译Ogre时遇到了很多坑,也学到了很多知识,写下来供以后查看和给大家分享。

环境搭建目标:可运行Ogre程序(废话),可在源代码中进行调试

遇到的问题:

1、在CMake Ogre时提示少工程依赖的源码:

答:首先要编译Dependencies。这也是开源大工程普遍做法,把其他依赖的开源库(源代码或是编译好的include+dll+lib)放在一个Dependencies文件中,给CMake congfigure和generate。一定要保证CMake时配置列表都变白,不能有红色。

 

2、CMake Dependencies时提示少SDL2:

答:需要下载SDL2源代码放在Dependencies的src中的相应位置。

 

3、编译好Dependencies,再编译Ogre时提示Obj文件的_MSC_VER不对

答:这是因为编译Dependencies和编译Ogre时使用的VS平台工具集不一样。

 

4、如何获得不同版本的VS平台工具集

答:只能下载对应的VS

 

5、如何在VS中跨解决方案进行调试

答:这个可以实现,只要把编译Ogre debug版本时的pdb拷到目标解决方案的工作目录里边就行

 

6、如何在VS中跨解决方案在头文件中按F12查看源代码

答:这个实现不了,因为在目标解决方案中,只能引用另一个解决方案的include、lib和使用另一个解决方案的dll。拿不到另一个解决方案的cpp。除非是vs帮我们生成的pdb中,有cpp的副本。所以实现按F12查看源代码只能再同一个解决方案下。

 

7、在编译官方ogre tutorial framework时,报Link错误

1>Main.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: static void * __cdecl Ogre::NedPoolingPolicy::allocateBytes(unsigned int,char const *,int,char const *)" (__imp_?allocateBytes@NedPoolingPolicy@Ogre@@SAPAXIPBDH0@Z),该符号在函数 "public: struct std::_Container_proxy * __thiscall Ogre::STLAllocator<struct std::_Container_proxy,class Ogre::CategorisedAllocPolicy<0> >::allocate(unsigned int,void const *)" (?allocate@?$STLAllocator@U_Container_proxy@std@@V?$CategorisedAllocPolicy@$0A@@Ogre@@@Ogre@@QAEPAU_Container_proxy@std@@IPBX@Z) 中被引用
1>Main.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: static void __cdecl Ogre::NedPoolingPolicy::deallocateBytes(void *)" (__imp_?deallocateBytes@NedPoolingPolicy@Ogre@@SAXPAX@Z),该符号在函数 "public: void __thiscall Ogre::STLAllocator<struct std::_Container_proxy,class Ogre::CategorisedAllocPolicy<0> >::deallocate(struct std::_Container_proxy *,unsigned int)" (?deallocate@?$STLAllocator@U_Container_proxy@std@@V?$CategorisedAllocPolicy@$0A@@Ogre@@@Ogre@@QAEXPAU_Container_proxy@std@@I@Z) 中被引用
1>E:\ogre\OgreLearn\Debug\OgreLearn.exe : fatal error LNK1120: 2 个无法解析的外部命令

google了一下午也没找到,自己注释代码,找错误语句,分析了半天代码才明白。

出错的代码是Ogre::StringVector v; 这句话,当ogre要给如vector,queue,map,mutipleMap这样的数据结构分配内存时,分配的方式有4种,在OgreConfig中定义:

#define OGRE_MEMORY_ALLOCATOR_STD 1
#define OGRE_MEMORY_ALLOCATOR_NED 2
#define OGRE_MEMORY_ALLOCATOR_USER 3
#define OGRE_MEMORY_ALLOCATOR_NEDPOOLING 4

NEDPOOLING这种内存分配方式的函数没有在OgreMain_d.lib中,所以报了那个错误。

官方Sample没有报错的方式是因为把内存分配方式设置为了1,而自己建的工程,默认是4。

修改的地方时自己工程的外部依赖项中的OgreBuildSetting.h,把OGRE_MEMORY_ALLOCATOR调成1就可以了

原来在同一个解决方案中,不同工程引用同一个外部依赖项文件,那个文件的代码也可以是不一样的。