【问题标题】:Boost::Test -- generation of Main()?Boost::Test -- Main() 的生成?
【发布时间】:2009-08-09 10:56:13
【问题描述】:

我对设置 boost 测试库有点困惑。这是我的代码:

#include "stdafx.h"
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE pevUnitTest
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE( TesterTest )
{
    BOOST_CHECK(true);
}

我的编译器生成了非常有用的错误消息:

1>MSVCRTD.lib(wcrtexe.obj) : error LNK2019: unresolved external symbol _wmain referenced in function ___tmainCRTStartup
1>C:\Users\Billy\Documents\Visual Studio 10\Projects\pevFind\Debug\pevUnitTest.exe : fatal error LNK1120: 1 unresolved externals

似乎 Boost::Test 库没有生成 main() 函数——我的印象是每当定义 BOOST_TEST_MODULE 时它都会这样做。但是......链接器错误仍在继续。

有什么想法吗?

比利3

编辑:这是我的代码来解决下面正确答案中描述的错误:

#include "stdafx.h"
#define BOOST_TEST_MODULE pevUnitTests
#ifndef _UNICODE
#define BOOST_TEST_MAIN
#endif
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

#ifdef _UNICODE

int _tmain(int argc, wchar_t * argv[])
{
    char ** utf8Lines;
    int returnValue;

    //Allocate enough pointers to hold the # of command items (+1 for a null line on the end)
    utf8Lines = new char* [argc + 1];

    //Put the null line on the end (Ansi stuff...)
    utf8Lines[argc] = new char[1];
    utf8Lines[argc][0] = NULL;

    //Convert commands into UTF8 for non wide character supporting boost library
    for(unsigned int idx = 0; idx < argc; idx++)
    {
        int convertedLength;
        convertedLength = WideCharToMultiByte(CP_UTF8, NULL, argv[idx], -1, NULL, NULL, NULL, NULL);
        if (convertedLength == 0)
            return GetLastError();
        utf8Lines[idx] = new char[convertedLength]; // WideCharToMultiByte handles null term issues
        WideCharToMultiByte(CP_UTF8, NULL, argv[idx], -1, utf8Lines[idx], convertedLength, NULL, NULL);
    }

    //From boost::test's main()
    returnValue = ::boost::unit_test::unit_test_main( &init_unit_test, argc, utf8Lines );
    //End from boost::test's main()

    //Clean up our mess
    for(unsigned int idx = 0; idx < argc + 1; idx++)
        delete [] utf8Lines[idx];
    delete [] utf8Lines;

    return returnValue;
}

#endif

BOOST_AUTO_TEST_CASE( TesterTest )
{
    BOOST_CHECK(false);
}

希望对某人有所帮助。

比利3

【问题讨论】:

    标签: c++ visual-studio-2010 boost-test


    【解决方案1】:

    我认为问题在于您使用的是 VC10 测试版。

    它有一个有趣的小错误,当启用 Unicode 时,它​​要求入口点是 wmain,而不是 main。 (在这些情况下,旧版本允许您同时使用 wmainmain)。

    当然,这将在下一个测试版中得到解决,但在那之前,这是一个问题。 :)

    您可以降级到 VC9、禁用 Unicode,或尝试在项目属性中手动将入口点设置为 main

    可能起作用的另一件事是,如果您定义自己的 wmain 存根,它调用 main。我很确定这在技术上是未定义的行为,但作为未发布编译器中编译器错误的解决方法,它可能会奏效。

    【讨论】:

    • 是的——就是这样。奇怪的是——我没有使用VC10——我已经将工具链设置为使用VS2008——我只使用VS2010作为它的编辑器。谢谢你的通灵调试。我现在正在研究最后一个选项,完成后会在这里发布。 (应用程序是 unicode——在 ANSI 模式下不值得测试)
    • 就我而言(VC++ 2008,Windows CE 控制台应用程序),只需将入口点链接器选项从 mainWCRTStartup 更改为 mainCRTStartup。
    猜你喜欢
    • 2020-03-08
    • 2011-05-07
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    • 1970-01-01
    • 2014-10-07
    • 1970-01-01
    相关资源
    最近更新 更多