Converting std::__cxx11::string to std::string

后端 未结 8 1763
春和景丽
春和景丽 2020-11-22 16:07

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 t

相关标签:
8条回答
  • 2020-11-22 16:46

    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.

    Easiest way to check dependencies is to perform 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 using clang++. 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 use libc++.so.

    • Build library without extra options and link binary to -lstdc++. In this case both library and app will use libstdc++.so.

    0 讨论(0)
  • 2020-11-22 16:54

    In my case, I was having a similar problem:

    /usr/bin/ld: Bank.cpp:(.text+0x19c): undefined reference to 'Account::SetBank(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)' collect2: error: ld returned 1 exit status
    

    After some researches, I realized that the problem was being generated by the way that Visual Studio Code was compiling the Bank.cpp file. So, to solve that, I just prompted the follow command in order to compile the c++ file sucessful:

    g++ Bank.cpp Account.cpp -o Bank
    

    With the command above, It was able to linkage correctly the Header, Implementations and Main c++ files.

    OBS: My g++ version: 9.3.0 on Ubuntu 20.04

    0 讨论(0)
  • 2020-11-22 16:58

    Is it possible that you are using 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.

    Source: GCC 5 Release Notes/Dual ABI

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

    0 讨论(0)
  • 2020-11-22 17:02

    For me -D_GLIBCXX_USE_CXX11_ABI=0 didn't help.

    It works after I linked to C++ libs version instead of gnustl.

    0 讨论(0)
  • 2020-11-22 17:03

    If you can recompile all incompatible libs you use, do it with compiler option

    -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

    -D_GLIBCXX_USE_CXX11_ABI=0

    The define

    #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.

    0 讨论(0)
  • 2020-11-22 17:03

    I had a similar issue recently while trying to link with the pre-built binaries of hdf5 version 1.10.5 on 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 website for your particular distribution and build the library. It is very easy.

    You will also need the compression libraries zlib and szip from here and here, respectively, if you do not already have them on your system.

    0 讨论(0)
提交回复
热议问题