Nal*_*hri 5 c++ boost boost-propertytree istringstream c++14
我有一个 .ini 文件包含以下数据
[SYSTEM]
num_of_vps = 1
我有这段代码来读取 .ini 文件中的元素。(uint定义为typedef unsigned int uint)
boost::property_tree::ptree pt; 
boost::property_tree::ini_parser::read_ini(iniFilePath, pt);
hwCount = pt.get<uint>("SYSTEM.num_of_vps"); 
我从包含上述代码的文件创建了一个文件,并在文件中的包装函数中调用了它main.cc。然后我收到以下错误
抛出 'boost_1_68_0::exception_detail::clone_impl<boost_1_68_0::exception_detail::error_info_injector<boost_1_68_0::property_tree::ptree_bad_data> >' 实例后调用的终止what():将数据转换为类型“j”失败的堆栈跟踪
#12 0x00002aaab613fcd5 in abort () from /lib64/libc.so.6
#13 0x00002aaab9b29315 in __gnu_cxx::__verbose_terminate_handler () at ../../../../src/gcc-7.3.0/libstdc++-v3/libsupc++/vterminate.cc:95
#14 0x00002aaab9a9e8f6 in __cxxabiv1::__terminate (handler=<optimized out>) at ../../../../src/gcc-7.3.0/libstdc++-v3/libsupc++/eh_terminate.cc:47
#15 0x00002aaab9a9e941 in std::terminate () at ../../../../src/gcc-7.3.0/libstdc++-v3/libsupc++/eh_terminate.cc:57
#16 0x00002aaab9a9ea74 in __cxxabiv1::__cxa_throw (obj=<optimized out>, tinfo=0x2aaab9e1ff60 <typeinfo for boost_1_68_0::exception_detail::clone_impl<boost_1_68_0::exception_detail::error_info_injector<boost_1_68_0::property_tree::ptree_bad_data> >>, dest=0x2aaab99bef18 <boost_1_68_0::exception_detail::clone_impl<boost_1_68_0::exception_detail::error_info_injector<boost_1_68_0::property_tree::ptree_bad_data> >::~clone_impl()>) at ../../../../src/gcc-7.3.0/libstdc++-v3/libsupc++/eh_throw.cc:93
#17 0x00002aaab99bec82 in boost_1_68_0::throw_exception<boost_1_68_0::exception_detail::error_info_injector<boost_1_68_0::property_tree::ptree_bad_data> > (e=...) at throw_exception.hpp:72
#18 0x00002aaab99be576 in boost_1_68_0::exception_detail::throw_exception_<boost_1_68_0::property_tree::ptree_bad_data> (x=..., current_function=0x2aaab9b45fc0 <boost_1_68_0::enable_if<boost_1_68_0::property_tree::detail::is_translator<boost_1_68_0::property_tree::stream_translator<char, std::char_traits<char>, std::allocator<char>, unsigned int> >, unsigned int>::type boost_1_68_0::property_tree::basic_ptree<std::string, std::string, std::less<std::string> >::get_value<unsigned int, boost_1_68_0::property_tree::stream_translator<char, std::char_traits<char>, std::allocator<char>, unsigned int> >(boost_1_68_0::property_tree::stream_translator<char, std::char_traits<char>, std::allocator<char>, unsigned int>) const::__PRETTY_FUNCTION__> "typename boost_1_68_0::enable_if<boost_1_68_0::property_tree::detail::is_translator<Translator>, Type>::type boost_1_68_0::property_tree::basic_ptree<Key, Data, KeyCompare>::get_value(T"..., file=0x2aaab9b45830 "property_tree/detail/ptree_implementation.hpp", line=675) at throw_exception.hpp:89
#19 0x00002aaab99be01e in boost_1_68_0::property_tree::basic_ptree<std::string, std::string, std::less<std::string> >::get_value<unsigned int, boost_1_68_0::property_tree::stream_translator<char, std::char_traits<char>, std::allocator<char>, unsigned int> > (this=0xbc86c8, tr=...) at property_tree/detail/ptree_implementation.hpp:673
#20 0x00002aaab99bd6a5 in boost_1_68_0::property_tree::basic_ptree<std::string, std::string, std::less<std::string> >::get_value<unsigned int> (this=0xbc86c8) at property_tree/detail/ptree_implementation.hpp:683
#21 0x00002aaab99bc545 in boost_1_68_0::property_tree::basic_ptree<std::string, std::string, std::less<std::string> >::get<unsigned int> (this=0x7fffffff9470, path=...) at property_tree/detail/ptree_implementation.hpp:754
#22 0x00002aaab99bba83 in MyRT::DUTConfigFile::readIniFile (this=0xbc5d50, iniFilePath=...) at DUTConfigFile.cpp:231
#23 0x00002aaab99ba8d2 in MyRT::DUTConfigFile::DUTConfigFile (this=0xbc5d50, iniFilePath=..., configFilePath=...) at DUTConfigFile.cpp:26
#24 0x00002aaab99c0839 in setupMyConfigs () at SimXLInterface.cpp:83
#31 0x0000000000408847 in main ()
我尝试了 gdb,当使用 istringstream 将字符串转换为 uint 时,它抛出异常。下面是两种类型的转换
template<class K, class D, class C>                                                                                                                    
template<class Type> inline                                                                                                                            
Type basic_ptree<K, D, C>::get_value() const
{
    return get_value<Type>(
        typename translator_between<data_type, Type>::type());
}
(gdb) p typeid(Type).name()
$2 = 0x2aaab5c33c91 <typeinfo name for unsigned int> "j"
(gdb) p typeid(data_type).name()
could not find typeinfo symbol for 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >'
下面的函数basic_ios.h抛出__throw_bad_cast()异常(此处this->_M_num_get = NULL)
const __num_get_type& __ng = __check_facet(this->_M_num_get);
默认情况下,内部构建自动化 Makefile 添加-D_GLIBCXX_USE_CXX11_ABI=0到目标文件创建中
g++ -I/usr/myboost/boost/boost -Wall -std=c++14 -fopenmp -m64 -msse2 -fPIC \
  -D_GLIBCXX_USE_CXX11_ABI=0 -g -o *.o -fPIC -c *.cpp
.so 构建命令
g++ -m64 -msse2 -pthread -shared -static-libstdc++ -static-libgcc  \
  -Wl,-znoexecstack -g -o runtime.so *.o -ldl -lrt -lz -fopenmp -lcrypto
如果我将其更改为读取 std::string 类型,然后将其转换为 uint,则它可以毫无异常地工作
std::string strHwCount = pt.get<std::string>("SYSTEM.num_of_vps");
hwCount = static_cast<uint>(std::stoul(strHwCount));
我正在使用boost-1.68和gcc-7.3。ABI这与我使用的宏有关吗?有没有办法在没有上述解决方法的情况下解决这个问题
首先,我将项目从 cmake 迁移到内部自定义构建自动化(Makefile 的某些增强版本)。在 cmake 中这工作得很好。根据sehe的回答我做了一些进一步的分析。在一个小的独立示例中,此错误是不可重现的(使用本地字符串和实际读取的 .ini 文件)。
当通过gdb时,在文件中,我可以在和中boost/property_tree/stream_translator.hpp找到从文件中读取的值(更改为num_of_vps = 4文件中)。stringistringstream
optional<Type> basic_ptree<K, D, C>
    ::get_value_optional(Translator tr) const
{
    return tr.get_value(data());
}
(gdb) p data()
$1 = "4"
boost_1_68_0::optional<E> get_value(const internal_type &v) {
    std::basic_istringstream<Ch, Traits, Alloc> iss(v);
    iss.imbue(m_loc);
    E e;
    customized::extract(iss, e);
    ....
}
(gdb) p v
$1 = "4"
(gdb) p iss.str()
$2 = "4"
在上面的customized::extract(iss, e)函数内部有一个使用 astring的转换unsigned intbasic_istream
static void extract(std::basic_istream<Ch, Traits>& s, E& e) {
    s >> e;
    if(!s.eof()) { s >> std::ws; }
} 
该>>运算符内部有一个_M_extract函数,该函数内部__check_facet抛出异常(通过检查 NULL this->_M_num_get)
basic_istream<_CharT, _Traits>::
_M_extract(_ValueT& __v) {
  __try {
    const __num_get_type& __ng = __check_facet(this->_M_num_get);
    ...
inline const _Facet&
__check_facet(const _Facet* __f) {
  if (!__f)
    __throw_bad_cast();
  return *__f;
}
上述场景的堆栈跟踪
#0  std::istream::_M_extract<unsigned int>(unsigned int&) (__f=0x0) at gcc-7.3.0/objdir/x86_64-centos-linux/libstdc++-v3/include/bits/basic_ios.h:49
#1  std::istream::operator>>(unsigned int&) (this=0x7fffffff9980, __n=@0x7fffffff997c: 0)
#2  boost_1_68_0::property_tree::customize_stream::extract(std::istream&, unsigned int&) (s=..., e=@0x7fffffff997c: 0)
#3  boost_1_68_0::property_tree::stream_translator<char, std::char_traits<char>, std::allocator<char>, unsigned int>::get_value(std::string const&) (this=0x7fffffff9b58, v="4")
我可以在我们正在使用的另一个地方看到相同的行为std::stringstream 。ss即使<<操作员添加字符串,变量也显示为空
std::stringstream ss;
ss << std::setw(8) << std::setfill('0') << std::hex << firmId;
我担心该ABI标志会导致此问题,但我能够将其删除,但问题仍然存在。我搜索了此类istream问题,但找不到任何有用的东西。
通过对 gcc 命令进行调整,我能够使用.so所有sstream用法来运行可执行文件。以前的 gcc 链接命令是
g++ -m64 -msse2 -pthread -shared -static-libstdc++ -static-libgcc  \
  -Wl,-znoexecstack -g -o runtime.so *.o -ldl -lrt -lz -fopenmp -lcrypto
删除-static-libstdc++和动态链接libstdc++.so有效
g++ -m64 -msse2 -pthread -shared -static-libgcc -Wl,-znoexecstack -o runtime.so *.o \
  -fPIC -ldl -lrt -lz -fopenmp -lcrypto -Wl,-rpath,/depot/gcc-7.3.0/lib64