C++ 将 std::__cxx11::string 转换为 std::string
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33394934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting std::__cxx11::string to std::string
提问by jorgen
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.
我使用c++11,但也有一些库没有为它配置,需要一些类型转换。特别是我需要一种转换std::__cxx11::string
为常规的方法std::string
,但是谷歌搜索我找不到这样做的方法并且放在(string)
前面不起作用。
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'
回答by Matth?us Brandl
Is it possible that you are using GCC 5?
您是否可能正在使用 GCC 5?
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.
如果您收到有关未定义引用符号的链接器错误,这些符号涉及 std::__cxx11 命名空间或标记 [abi:cxx11] 中的类型,那么这可能表明您正在尝试将使用 _GLIBCXX_USE_CXX11_ABI 的不同值编译的目标文件链接在一起宏。当链接到使用旧版 GCC 编译的第三方库时,通常会发生这种情况。如果无法使用新 ABI 重建第三方库,则需要使用旧 ABI 重新编译代码。
Source: GCC 5 Release Notes/Dual ABI
Defining the following macro beforeincluding any standard library headers should fix your problem: #define _GLIBCXX_USE_CXX11_ABI 0
在包含任何标准库头文件之前定义以下宏应该可以解决您的问题:#define _GLIBCXX_USE_CXX11_ABI 0
回答by Denis Sirotkin
If you can recompile all incompatible libs you use, do it with compiler option
如果您可以重新编译您使用的所有不兼容的库,请使用编译器选项进行编译
-D_GLIBCXX_USE_CXX11_ABI=1
-D_GLIBCXX_USE_CXX11_ABI=1
and then rebuild your project. If you can't do so, add to your project's makefile compiler option
然后重建你的项目。如果你不能这样做,添加到你的项目的 makefile 编译器选项
-D_GLIBCXX_USE_CXX11_ABI=0
-D_GLIBCXX_USE_CXX11_ABI=0
The define
定义
#define _GLIBCXX_USE_CXX11_ABI 0/1
#define _GLIBCXX_USE_CXX11_ABI 0/1
is also good but you probably need to add it to all your files while compiler option do it for all files at once.
也很好,但您可能需要将它添加到所有文件中,而编译器选项一次为所有文件执行此操作。
回答by rightaway717
Answers here mostly focus on short way to fix it, but if that does not help, I'll give some steps to check, that helped me (Linux only):
这里的答案主要集中在解决它的简短方法上,但如果这没有帮助,我会给出一些步骤来检查,这对我有帮助(仅限 Linux):
- If the linker errors happen when linking other libraries, build those libs with debug symbols ("-g" GCC flag)
List the symbols in the library and grep the symbols that linker complains about (enter the commands in command line):
nm lib_your_problem_library.a | grep functionNameLinkerComplainsAbout
If you got the method signature, proceed to the next step, if you got
no symbols
instead, mostlikely you stripped off all the symbols from the library and that is why linker can't find them when linking the library. Rebuild the library without stripping ALL the symbols, you can strip debug (strip -S
option) symbols if you need.Use a c++ demangler to understand the method signature, for example, this one
- Compare the method signature in the library that you just got with the one you are using in code (check header file as well), if they are different, use the proper header or the proper library or whatever other way you now know to fix it
- 如果链接其他库时发生链接器错误,请使用调试符号(“-g”GCC 标志)构建这些库
列出库中的符号并 grep 链接器抱怨的符号(在命令行中输入命令):
nm lib_your_problem_library.a | grep functionNameLinkerComplainsAbout
如果您获得了方法签名,请继续下一步,如果您获得了
no symbols
,很可能您从库中剥离了所有符号,这就是链接器在链接库时找不到它们的原因。重建库而不剥离所有符号,strip -S
如果需要,您可以剥离调试(选项)符号。使用c++ demangler来理解方法签名,例如这个
- 将您刚刚获得的库中的方法签名与您在代码中使用的方法签名(也请检查头文件)进行比较,如果它们不同,请使用正确的头文件或正确的库或您现在知道的任何其他方法来修复它
回答by Evgen Bodunov
When I had similar issue it's happened because my lib was build using clang++
, and it's linked to libstdc++.so
by default on my system. While app binary was build using clang
and linked with -lc++
option.
当我遇到类似问题时,这是因为我的库是使用 构建的clang++
,并且libstdc++.so
默认情况下它在我的系统上链接到。虽然应用程序二进制文件是使用选项构建clang
并链接的-lc++
。
Easiest way to check dependencies is to perform ldd libName.so
检查依赖关系的最简单方法是执行 ldd libName.so
To fix it you should use the same library on in app and library.
要修复它,您应该在应用程序和库中使用相同的库。
Easiest way. Build library using
clang++
and compile app usingclang++
. Without extra linking options on both steps. Default stdlib will be used.Build library with
-stdlib=c++
and compile app with-lc++
. In this case both library and app will uselibc++.so
.Build library without extra options and link binary to
-lstdc++
. In this case both library and app will uselibstdc++.so
.
最简单的方法。使用构建库
clang++
并使用clang++
. 两个步骤都没有额外的链接选项。将使用默认标准库。构建库
-stdlib=c++
,并编译应用程序-lc++
。在这种情况下,库和应用程序都将使用libc++.so
.无需额外选项即可构建库并将二进制文件链接到
-lstdc++
. 在这种情况下,库和应用程序都将使用libstdc++.so
.
回答by unbound37
I had a similar issue recently while trying to link with the pre-built binaries of hdf5 version 1.10.5on Ubuntu 16.04. None of the solutions suggested here worked for me, and I was using g++ version 9.1. I found that the best solution is to build the hdf5 library from source. Do not use the pre-built binaries since these were built using gcc 4.9! Instead, download the source code archives from the hdf websitefor your particular distribution and build the library. It is very easy.
我最近在尝试与Ubuntu 16.04 上的hdf5 版本 1.10.5的预构建二进制文件链接时遇到了类似的问题。这里建议的所有解决方案都不适合我,我使用的是 g++ 9.1 版。我发现最好的解决方案是从源代码构建 hdf5 库。不要使用预先构建的二进制文件,因为它们是使用 gcc 4.9 构建的!相反,从 hdf网站为您的特定发行版下载源代码档案并构建库。这很容易。
You will also need the compression libraries zliband szipfrom hereand here, respectively, if you do not already have them on your system.
回答by ceorron
I got this, the only way I found to fix this was to update all of mingw-64 (I did this using pacman on msys2 for your information).
我明白了,我发现解决此问题的唯一方法是更新所有 mingw-64(我在 msys2 上使用 pacman 进行了此操作以供您参考)。
回答by dimon4eg
For me -D_GLIBCXX_USE_CXX11_ABI=0 didn't help.
对我来说 -D_GLIBCXX_USE_CXX11_ABI=0 没有帮助。
It works after I linked to C++ libs version instead of gnustl.
它在我链接到 C++ libs 版本而不是 gnustl 后工作。