将 std::__cxx11::string 转换为 std::string

2021-12-05 00:00:00 string types c++ c++11 std

我用的是c++11,但也有一些库没有为它配置,需要一些类型转换.特别是我需要一种将 std::__cxx11::string 转换为常规 std::string 的方法,但是谷歌搜索我找不到这样做的方法并把前面的(string)不起作用.

I use c++11, but also some libraries that are not configured for it, and need some type conversion. In particular I need a way to convert std::__cxx11::string to regular std::string, but googling I can't find a way to do this and putting (string) in front does not work.

如果我不转换,我会收到这样的链接器错误:

If I do not convert I get linker errors like this:

undefined reference to `H5::CompType::insertMember(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned long, H5::DataType const&) const'

推荐答案

您是否可能使用 GCC 5?

Is it possible that you are using GCC 5?

如果您收到有关未定义引用符号的链接器错误,这些符号涉及 std::__cxx11 命名空间或标记 [abi:cxx11] 中的类型,那么这可能表明您正在尝试将使用不同值编译的目标文件链接在一起对于 _GLIBCXX_USE_CXX11_ABI 宏.当链接到使用旧版 GCC 编译的第三方库时,通常会发生这种情况.如果无法使用新 ABI 重建第三方库,则需要使用旧 ABI 重新编译代码.

If you get linker errors about undefined references to symbols that involve types in the std::__cxx11 namespace or the tag [abi:cxx11] then it probably indicates that you are trying to link together object files that were compiled with different values for the _GLIBCXX_USE_CXX11_ABI macro. This commonly happens when linking to a third-party library that was compiled with an older version of GCC. If the third-party library cannot be rebuilt with the new ABI then you will need to recompile your code with the old ABI.

来源:GCC 5 发行说明/双 ABI

定义以下宏之前包括任何标准库头应该可以解决您的问题:#define _GLIBCXX_USE_CXX11_ABI 0

Defining the following macro before including any standard library headers should fix your problem: #define _GLIBCXX_USE_CXX11_ABI 0

相关文章