Windows下编译MOCKCPP经验总结

来源:互联网 发布:linux下chromium加速 编辑:程序博客网 时间:2024/06/06 02:39

昨天成功的在Fedora10上编译了mockcpp-2.2-20090903,今天试着在Windows上编译,结果又碰到了很多玄机。

编译器:VC++2008 Express Edition(这是一个免费的版本,可以在微软网站上下载)

 

MOCKCPP发布的源代码中包括msvc目录,下面就是VC工程的文件,直接使用该工程文件就可以编译成功,生成mockcpp.lib。再生成一个简单的Windows控制台工程,通过cxxtest,引用mockcpp.lib,代码如下:

 


#include <cxxtest/TestSuite.h>
#include <mockcpp/mockable.h>
#include <mockcpp/mokc.h>

int your_fun(int input);

int my_fun(int input)
{
    int result = 0;

    MOCKABLE(your_fun);
    result = 3 * your_fun(input);
    return result;
}

class SimpleTest : public CxxTest::TestSuite
{
public:
    void testEquality()
    {
        MOCKER(your_fun)
        .defaults()
        .will(returnValue((int)0));

        int r = my_fun(1);
        TS_ASSERT_EQUALS( 0, r );
    }
};


 

上述代码编译可以通过,但链接时出现如下错误:

 


1>cxxtestrun.obj : error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl mockcpp::getDemangledName(class type_info const &)" (?getDemangledName@mockcpp@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABVtype_info@@@Z) referenced in function "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl mockcpp::TypeString<int>::value(void)" (?value@?$TypeString@H@mockcpp@@SA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ)
1>cxxtestrun.obj : error LNK2019: unresolved external symbol "void __cdecl mockcpp::assertFalse(unsigned int,char const *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,bool)" (?assertFalse@mockcpp@@YAXIPBDABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@_N@Z) referenced in function "private: virtual int __thiscall mockcpp::ChainableMockMethod<int>::getResult(class mockcpp::Any const &)" (?getResult@?$ChainableMockMethod@H@mockcpp@@EAEHABVAny@2@@Z)
1>cxxtestrun.obj : error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl mockcpp::toString(int)" (?toString@mockcpp@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@Z) referenced in function "public: virtual class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall mockcpp::Holder<int>::toString(void)const " (?toString@?$Holder@H@mockcpp@@UBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ)
1>cxxtestrun.obj : error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl mockcpp::toBufferString(void *,unsigned int)" (?toBufferString@mockcpp@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PAXI@Z) referenced in function "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl mockcpp::toString<struct mockcpp::Void>(struct mockcpp::Void)" (??$toString@UVoid@mockcpp@@@mockcpp@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@UVoid@0@@Z)
1>cxxtestrun.obj : error LNK2019: unresolved external symbol "struct mockcpp::Stub * __cdecl mockcpp::returnValue(class mockcpp::Any const &)" (?returnValue@mockcpp@@YAPAUStub@1@ABVAny@1@@Z) referenced in function "public: virtual void __thiscall SimpleTest::setUp(void)" (?setUp@SimpleTest@@UAEXXZ)


使用dumpbin查看mockcpp.lib中的内容,上述函数的定义都是存在的,但是为什么编译不过呢?

 

将生成mockcpp.lib所有的源文件加入前述的控制台工程替换对mockcpp.lib的引用,可以正常编译和链接。

 

在Fedora 10上,对MOCKCPP无论是以静态库还是源代码的形式引用,都没有问题;而在windows上,MOCKCPP以源代码的形式引用没有问题,而以静态库的方式引用时会出现链接错误(估计是VC++生成静态库时,对模板的处理能力不足)。由此可见VC++在某些方面的处理能力,与GCC相比还是有差距的,在实际的应用中需要注意。

 

原创粉丝点击