在 std c++11 中使用 Boost Test Unit 进行编译

Compilation using Boost Test Unit in std c++11

我正在尝试使用 Boost 测试单元编译一个非常简单的程序

#define BOOST_TEST_MODULE My Test
#include <boost/test/included/unit_test.hpp>

BOOST_AUTO_TEST_CASE(first_test) {   int i = 1;   BOOST_CHECK(i == 1); }

如果我不带参数编译这个小程序,

g++ test1.cpp

没问题。但是,如果我尝试使用 C++11 标准,

g++ test1.cpp -std=c++11

我遇到一些错误:

In file included from /usr/include/boost/test/included/unit_test.hpp:19:0,
                 from test1.cpp:2: /usr/include/boost/test/impl/debug.ipp: En la función ‘const char* boost::debug::{anónimo}::prepare_gdb_cmnd_file(const boost::debug::dbg_startup_info&)’: /usr/include/boost/test/impl/debug.ipp:426:23: error: ‘::mkstemp’ no se ha declarado
     fd_holder cmd_fd( ::mkstemp( cmd_file_name ) );
                       ^ In file included from /usr/include/boost/test/included/unit_test.hpp:19:0,
                 from test1.cpp:2: /usr/include/boost/test/impl/debug.ipp: En la función ‘bool boost::debug::attach_debugger(bool)’: /usr/include/boost/test/impl/debug.ipp:863:34: error: ‘::mkstemp’ no se ha declarado
     fd_holder init_done_lock_fd( ::mkstemp( init_done_lock_fn ) );
                                  ^ In file included from /usr/include/boost/test/utils/runtime/cla/dual_name_parameter.hpp:19:0,
                 from /usr/include/boost/test/impl/unit_test_parameters.ipp:31,
                 from /usr/include/boost/test/included/unit_test.hpp:33,
                 from test1.cpp:2: /usr/include/boost/test/utils/runtime/config.hpp: En la función ‘void boost::runtime::putenv_impl(boost::runtime::cstring, boost::runtime::cstring)’: /usr/include/boost/test/utils/runtime/config.hpp:95:51: error: ‘putenv’ no se declaró en este ámbito
     putenv( const_cast<char*>( fs.str().c_str() ) );

(编译器为西班牙语)

我正在使用:

欢迎任何帮助。谢谢。 何塞.-

看起来是 Cygwin 的东西。我无法在带有 Boost 1.54 的 OpenSUSE 13.2 i586 上重现它,但在带有 Boost 1.57 的 Cygwin Win32 上得到了与您相同的结果。而且,正如 Bo Persson 所建议的那样,也尝试了 std=gnu+11.

正如编译器所说“未 声明的 ”——即使你显式包含 <stdlib.h>,它同时声明了 mkstempputenv,——我似乎怀疑这完全是关于 C++ 语言扩展,而更像是头文件问题。事实上,在 Linux 中我们有:

#if defined __USE_MISC || defined __USE_XOPEN_EXTENDED \
    || defined __USE_XOPEN2K8
# ifndef __USE_FILE_OFFSET64
extern int mkstemp (char *__template) __nonnull ((1)) __wur;
# else
#  ifdef __REDIRECT
extern int __REDIRECT (mkstemp, (char *__template), mkstemp64)
     __nonnull ((1)) __wur;
#  else
#   define mkstemp mkstemp64
#  endif
# endif
# ifdef __USE_LARGEFILE64
extern int mkstemp64 (char *__template) __nonnull ((1)) __wur;
# endif
#endif

但在 Cygwin 中:

#ifndef __STRICT_ANSI__
#ifndef _REENT_ONLY
int _EXFUN(mkstemp,(char *));
#endif
int _EXFUN(_mkstemp_r, (struct _reent *, char *));
#endif

然后我在你的程序中添加了几个 #undefs:

#undef __STRICT_ANSI__
#undef _REENT_ONLY

#define BOOST_TEST_MODULE My Test
#include <boost/test/included/unit_test.hpp>

BOOST_AUTO_TEST_CASE(first_test) {   int i = 1;   BOOST_CHECK(i == 1); }

并且可以用 std=c++11 编译它。我不知道这可能有多不正确和愚蠢,但至少它产生了非常相似的 exe 文件,仅相差 20 个字节(指纹除外)。