Boost错误,编译xtime.hpp时出现问题。

7

我一直在使用Boost开发一个C++项目,在编译过程中,可能无意中升级了某个Boost组件,现在Boost的依赖关系无法编译:

In file included from /usr/include/boost/thread/pthread/mutex.hpp:14:0,
                 from /usr/include/boost/thread/mutex.hpp:16,
                 from /usr/include/boost/thread/pthread/thread_data.hpp:12,
                 from /usr/include/boost/thread/thread.hpp:17,
                 from /usr/include/boost/thread.hpp:13,
                 from /blah.h:4,
                 from bluh.h:3,
                 from bleh/main.cpp:4:
/usr/include/boost/thread/xtime.hpp:23:5: error: expected identifier before numeric constant
/usr/include/boost/thread/xtime.hpp:23:5: error: expected '}' before numeric constant
/usr/include/boost/thread/xtime.hpp:23:5: error: expected unqualified-id before numeric constant
/usr/include/boost/thread/xtime.hpp:46:14: error: expected type-specifier before 'system_time'
In file included from /usr/include/boost/thread/pthread/mutex.hpp:14:0,
                 from /usr/include/boost/thread/mutex.hpp:16,
                 from /usr/include/boost/thread/pthread/thread_data.hpp:12,
                 from /usr/include/boost/thread/thread.hpp:17,
                 from /usr/include/boost/thread.hpp:13,
                 from /blah,
                 from /bleh,(changed these names, obviously)
                 from /bluh /main.cpp:4:
/usr/include/boost/thread/xtime.hpp: In function 'int xtime_get(xtime*, int)':
/usr/include/boost/thread/xtime.hpp:73:40: error: 'get_system_time' was not declared in this scope
/usr/include/boost/thread/xtime.hpp:73:40: note: suggested alternative:
/usr/include/boost/thread/thread_time.hpp:19:24: note:   'boost::get_system_time'
/usr/include/boost/thread/xtime.hpp: At global scope:
/usr/include/boost/thread/xtime.hpp:88:1: error: expected declaration before '}' token
make[2]: *** [CMakeFiles/edge_based_tracker.dir/main.o] Error 1
make[1]: *** [CMakeFiles/edge_based_tracker.dir/all] Error 2
make: *** [all] Error 2

任何想法吗?我尝试将TIME_UTC更改为TIME_UTC_,因为这是另一个网站上推荐给我的,但似乎没有帮助。
编辑:Boost版本是版本:1.48.0.2。我在下面附上了xtime。
// Copyright (C) 2001-2003
// William E. Kempf
// Copyright (C) 2007-8 Anthony Williams
//
//  Distributed under the Boost Software License, Version 1.0. (See accompanying 
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef BOOST_XTIME_WEK070601_HPP
#define BOOST_XTIME_WEK070601_HPP

#include <boost/thread/detail/config.hpp>

#include <boost/cstdint.hpp>
#include <boost/thread/thread_time.hpp>
#include <boost/date_time/posix_time/conversion.hpp>

#include <boost/config/abi_prefix.hpp>

namespace boost {

enum xtime_clock_types
{
    TIME_UTC=1 //LINE 23
//    TIME_TAI,
//    TIME_MONOTONIC,
//    TIME_PROCESS,
//    TIME_THREAD,
//    TIME_LOCAL,
//    TIME_SYNC,
//    TIME_RESOLUTION
};

struct xtime
{
#if defined(BOOST_NO_INT64_T)
    typedef int_fast32_t xtime_sec_t; //INT_FAST32_MIN <= sec <= INT_FAST32_MAX
#else
    typedef int_fast64_t xtime_sec_t; //INT_FAST64_MIN <= sec <= INT_FAST64_MAX
#endif

    typedef int_fast32_t xtime_nsec_t; //0 <= xtime.nsec < NANOSECONDS_PER_SECOND

    xtime_sec_t sec;
    xtime_nsec_t nsec;

    operator system_time() const
    {
        return boost::posix_time::from_time_t(0)+
            boost::posix_time::seconds(static_cast<long>(sec))+
#ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
            boost::posix_time::nanoseconds(nsec);
#else
        boost::posix_time::microseconds((nsec+500)/1000);
#endif
    }

};

inline xtime get_xtime(boost::system_time const& abs_time)
{
    xtime res;
    boost::posix_time::time_duration const time_since_epoch=abs_time-boost::posix_time::from_time_t(0);

    res.sec=static_cast<xtime::xtime_sec_t>(time_since_epoch.total_seconds());
    res.nsec=static_cast<xtime::xtime_nsec_t>(time_since_epoch.fractional_seconds()*(1000000000/time_since_epoch.ticks_per_second()));
    return res;
}

inline int xtime_get(struct xtime* xtp, int clock_type)
{
    if (clock_type == TIME_UTC)
    {
        *xtp=get_xtime(get_system_time());
        return clock_type;
    }
    return 0;
}


inline int xtime_cmp(const xtime& xt1, const xtime& xt2)
{
    if (xt1.sec == xt2.sec)
        return (int)(xt1.nsec - xt2.nsec);
    else 
        return (xt1.sec > xt2.sec) ? 1 : -1;
}

} // namespace boost

#include <boost/config/abi_suffix.hpp>

#endif //BOOST_XTIME_WEK070601_HPP

编辑:为了更清楚,这段代码在导入boost/thread.hpp时出现了错误。


如果您能说明Boost的版本,并展示出现问题的代码,以及展示xtime.hpp文件中第23行的内容,并将您的代码简化为最小示例,那将会很有帮助。事实上,按照目前的形式,这个问题是无法回答的。 - Jonathan Wakely
我已经编辑了问题,包括Boost的版本和xtime.hpp。失败的代码只是一个导入语句。 - user650261
你不需要复制粘贴Boost代码。展示你自己的代码,尝试编译它。 - Igor R.
被要求查看第23行,因此我展示了Boost代码给Jonathan Wakely看。正如我在之前的评论中所说,失败的代码是一个导入语句,具体来说是boost/thread。 - user650261
3个回答

13

对于遇到同样问题并且在此处苦苦寻找解决方案的人们。

这是从noodlebox的链接(https://svn.boost.org/trac/boost/ticket/6940)中提取的:

您可以编辑 /usr/include/boost/thread/xtime.hpp(或者您的xtime.hpp位于的任何位置), 并将所有出现的 TIME_UTC 改为 TIME_UTC_ - 可能大约在23和71行附近。

即: sed -i 's/TIME_UTC/TIME_UTC_/g' /usr/include/boost/thread/xtime.hpp


5
sed -i 's/TIME_UTC/TIME_UTC_/g' /usr/include/boost/thread/xtime.hpp - adam
编译PCL 1.7.1和boost 1.49.0库时出现错误。这个解决了这个问题(但是导致了其他错误 - 哦,我的天!)。 - Bull
我相信您还需要更改 libs/thread/src/pthread/timeconv.inl,因为它也包含 boost::TIME_UTC。 - Waheed

7
如果在使用Boost时出现语法错误,您可能需要使用-std=c++11编译代码。
您可能已经读到了关于TIME_UTC问题的信息。这是使用c++11的副作用。在c++11中,TIME_UTC被定义为宏,因此您需要将所有实例替换为TIME_UTC_,或者只使用新版本(1.50.0+)的Boost,其中此问题已经得到修复。
请参见:https://svn.boost.org/trac/boost/ticket/6940

小提示:它是由lib C的time.h完成的C11,而不是C++11(它不仅限于C++)。 - DrYak

1
由于您没有展示您的代码,我们只能猜测。我的猜测是您在代码中定义了TIME_UTC宏。这个宏会干扰xtime.hpp头文件。

@user650261 预处理整个翻译单元并在预处理代码中找到此行。 - Igor R.
将TIME_UTC替换为TIME_UTC_,如@noodlebox所建议。 - Dhiren Hamal

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