Boost.Test在64位Windows上的应用

6

我正在尝试在Visual Studio 2010中使用Boost.Test,但是我遇到了一些问题。以下是源代码:

#include "stdafx.hpp"

#define BOOST_TEST_MODULE (main)
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE(morphology)
{
    BOOST_CHECK(true);
}

stdafx.hpp实际上是空的。该项目是作为一个空的C ++项目创建的,设置为构建应用程序,并设置目录以指向boost头文件和库。我使用/entry来指向由Boost.Test提供的main函数。

似乎有几件事情正在发生。首先,在构建时,链接器报告有关测试库中所有对象的警告,声称“存在.CRT节;可能存在未处理的静态初始化程序或终止程序”:

1>msvcprtd.lib(locale0_implib.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>morphology.obj : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>libboost_unit_test_framework-vc100-mt-gd-1_49.lib(unit_test_main.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>libboost_unit_test_framework-vc100-mt-gd-1_49.lib(framework.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>libboost_unit_test_framework-vc100-mt-gd-1_49.lib(test_tools.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>libboost_unit_test_framework-vc100-mt-gd-1_49.lib(unit_test_log.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>libboost_unit_test_framework-vc100-mt-gd-1_49.lib(unit_test_suite.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>libboost_unit_test_framework-vc100-mt-gd-1_49.lib(results_reporter.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>libboost_unit_test_framework-vc100-mt-gd-1_49.lib(results_collector.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>libboost_unit_test_framework-vc100-mt-gd-1_49.lib(unit_test_parameters.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>libboost_unit_test_framework-vc100-mt-gd-1_49.lib(unit_test_monitor.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>libboost_unit_test_framework-vc100-mt-gd-1_49.lib(debug.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>libboost_unit_test_framework-vc100-mt-gd-1_49.lib(progress_monitor.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>libboost_unit_test_framework-vc100-mt-gd-1_49.lib(plain_report_formatter.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
1>libboost_unit_test_framework-vc100-mt-gd-1_49.lib(xml_report_formatter.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators

在Art运行时,测试应用程序在Boost.Test库的main调用树下崩溃:

>   Pentachoron.Test.exe!std::list<boost::shared_ptr<boost::runtime::cla::parameter>,std::allocator<boost::shared_ptr<boost::runtime::cla::parameter> > >::begin()  Line 787 + 0x13 bytes   C++
    Pentachoron.Test.exe!boost::unit_test::for_each::begin<std::list<boost::shared_ptr<boost::runtime::cla::parameter>,std::allocator<boost::shared_ptr<boost::runtime::cla::parameter> > > >(const std::list<boost::shared_ptr<boost::runtime::cla::parameter>,std::allocator<boost::shared_ptr<boost::runtime::cla::parameter> > > & t, boost::mpl::bool_<1> __formal)  Line 107 + 0xf bytes  C++
    Pentachoron.Test.exe!boost::runtime::cla::parser::operator[](boost::unit_test::basic_cstring<char const > string_id)  Line 169 + 0x41 bytes C++
    Pentachoron.Test.exe!boost::unit_test::runtime_config::`anonymous namespace'::retrieve_parameter<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >(boost::unit_test::basic_cstring<char const > parameter_name, const boost::runtime::cla::parser & s_cla_parser, const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & default_value, const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & optional_value)  Line 216 + 0x34 bytes  C++
    Pentachoron.Test.exe!boost::unit_test::runtime_config::report_sink()  Line 470 + 0x8b bytes C++
    Pentachoron.Test.exe!boost::unit_test::results_reporter::`anonymous namespace'::results_reporter_impl::results_reporter_impl()  Line 59 + 0x31 bytes    C++
    Pentachoron.Test.exe!boost::unit_test::results_reporter::`anonymous namespace'::s_rr_impl()  Line 91 + 0x35 bytes   C++
    Pentachoron.Test.exe!boost::unit_test::results_reporter::get_stream()  Line 120 + 0x5 bytes C++
    Pentachoron.Test.exe!`boost::unit_test::unit_test_main'::`1'::catch$3()  Line 207 + 0x5 bytes   C++
    msvcr100d.dll!_CallSettingFrame()  Line 44  Asm
    msvcr100d.dll!__CxxCallCatchBlock(_EXCEPTION_RECORD * pExcept)  Line 1337 + 0x15 bytes  C++
    ntdll.dll!0000000077c50c21()    
    [Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll] 
    Pentachoron.Test.exe!boost::unit_test::unit_test_main(boost::unit_test::test_suite * (int, char * *)* init_func, int argc, char * * argv)  Line 179 C++
    Pentachoron.Test.exe!main(int argc, char * * argv)  Line 238    C++
    kernel32.dll!000000007753652d()     
    ntdll.dll!0000000077c2c521()    

请问有没有什么编译器标志我没有设置,或者像那样的一些愚蠢错误...


这个链接可能有助于解决您的问题:https://dev59.com/AWw15IYBdhLWcg3wkMrz。在谷歌上大致查看了一下,这是我目前能找到的最好的答案。 - MrGomez
是的,那也是我发现的第一件事。MSDN的建议似乎并没有什么帮助,因为即使是一个空项目,默认情况下也会链接C和C++运行时库。另外,通过使用/entry我已经“不使用/noentry”了。 - Ben Collins
@ildjarn:我没有。我正在使用在这里构建的64位库:http://boost.teeks99.com/。 - Ben Collins
我想到的另一件事是,我正在使用Platform SDK 7.1工具集,但teeks99的boost libs都有vc100后缀,据我所知,使用平台工具集构建boost的唯一方法是自定义jamfiles(呃)。对吗? - Ben Collins
同意@Soo的观点--我认为第一步是自己构建Boost。指南在这里(http://www.boost.org/more/getting_started/windows.html)。 - ildjarn
显示剩余4条评论
2个回答

10

好的,我想通了。问题是我将/entry设置为直接指向main函数,这会绕过所有静态对象构造函数和运行时初始化。一旦我设置/subsystem:console选项,并让编译器正确设置运行时并调用Boost.Test提供的main函数,它就像预期的那样工作。

真是一件麻烦事。

/subsystem:console参数是MSVC链接器使用的。要配置此内容,请右键单击项目,在链接器 | 命令行中添加/subsystem:console到对话框底部的附加选项中。(VS2013)


哇,我觉得你为我节省了很多时间...我也遇到了完全相同的问题,谢谢你。 - MOnsDaR

1

我在 x64 上使用 Boost.Test 没有问题。

您能否验证您应用程序中链接的 C 运行时库是否与 Boost 使用的运行时库匹配?根据您的链接器输出,Boost 显然正在使用 /MDd。 (请参见 MSDN)。


dumpbin /all .\libboost_unit_test_framework-vc100-mt-gd-1_49.lib | select-string DEFAULTLIB 的输出显示了 msvcprtdMSVCRTD。我在我的应用程序中使用 /MDd(它链接到 MSVCRTD)。 - Ben Collins
那么,你如何设置你的项目? - Ben Collins
根据Boost命名方案-mt-gd表示多线程调试(非静态链接)。这是否与默认设置(项目设置中的/MDd)相匹配? - Soo Wei Tan
我在我的stdafx.h中添加了#include <boost/test/unit_test.hpp>,在release中使用/MD,在debug中使用/MDd。我的入口点未设置,并且我没有使用/NOENTRY。Win32的交叉编译是否有效? - Soo Wei Tan
我不确定默认设置是什么,但我在 Debug 模式下使用 /MDd。 - Ben Collins

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接