Boost::Test——Main()的生成?

发布于 2024-08-01 12:43:21 字数 2291 浏览 7 评论 0原文

我对设置 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 时它都会执行此操作被定义为。 但是......链接器错误仍然存​​在。

有任何想法吗?

Billy3

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

#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

I'm a bit confused on setting up the boost test library. Here is my code:

#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);
}

My compiler generates the wonderfully useful error message:

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

It seems that the Boost::Test library is not generating a main() function -- I was under the impression it does this whenever BOOST_TEST_MODULE is defined. But ... the linker error continues.

Any ideas?

Billy3

EDIT: Here's my code to work around the bug described in the correct answer below:

#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);
}

Hope that's helpful to someone.

Billy3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏了南城 2024-08-08 12:43:21

我认为问题在于您使用的是 VC10 beta。

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

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

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

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

I think the problem is that you're using the VC10 beta.

It has a fun little bug where, when Unicode is enabled, it requires the entry point to be wmain, not main. (Older versions allowed you to use both wmain and main in those cases).

Of course this will be fixed in the next beta, but until then, well, it's a problem. :)

You can either downgrade to VC9, disable Unicode, or try manually setting the entry point to main in project properties.

Another thing that might work is if you define your own wmain stub, which calls main. I'm pretty sure this is technically undefined behavior, but as a workaround for a compiler bug in an unreleased compiler it might do the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文