How to handle dual ABI in GCC 5?

Aleph

I try to understand how to overcome problems with the dual ABI introduced in GCC 5. However, I don't manage to do it. Here is a very simple example to reproduce errors. The version of GCC I use is 5.2. As you can see, my main function (in main.cpp file) is quite simple:

// main.cpp

#include <iostream>
#include <string>

int main()
{
    std::string message = "SUCCESS!";
    std::cout << message << std::endl;
}

When I type

/home/aleph/gcc/5.2.0/bin/g++ main.cpp

I get the following error message:

/tmp/ccjsTADd.o: In function `main':
main.cpp:(.text+0x26): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'
main.cpp:(.text+0x43): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
main.cpp:(.text+0x5c): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
main.cpp:(.text+0x8c): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
collect2: erreur: ld a retourné 1 code d'état d'exécution

If I change the value of _GLIBCXX_USE_CXX11_ABI to 0, the problem disappears.

But, how to make things work with the default ABI?

EDIT: simpler question (removed cmake script)

Aleph

I found the solution by adding more verbosity to gcc (-v flag). If you have the same problem, you need to tell gcc to search for libraries in the repository containing the libstdc++ version of your distribution. In other words, you should try something like this:

/home/aleph/gcc/5.2.0/bin/g++ -L /home/aleph/gcc/5.2.0/lib64 main.cpp

The linking step should be correctly performed after that. However, you may not be able to run your program. Entering

./a.out

may lead to the following error:

./a.out: relocation error: ./a.out: symbol _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1EPKcRKS3_, version GLIBCXX_3.4.21 not defined in file libstdc++.so.6 with link time reference

Indeed, you can check that your executable depends on the wrong version of libstdc++ by typing

ldd a.out

which should lead to something like that:

linux-vdso.so.1 =>  (0x00007ffebb722000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x0000003a71400000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x0000003d03a00000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x0000003a71000000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x0000003d02e00000)
/lib64/ld-linux-x86-64.so.2 (0x0000003d02a00000)

Set LD_LIBRARY_PATH to the path to your own version of libstdc++ will solve the problem:

LD_LIBRARY_PATH=/home/aleph/gcc/5.2.0/lib64 ./a.out

Hope it helps!

EDIT: as noticed by Marc, it is possible to modify the rpath, instead of modifying the environment variable LD_LIBRARY_PATH. Here is a CMake configuration script to do it.

project (example CXX)

add_executable(main main.cpp)

if(GCC_ROOT)
    set(CMAKE_CXX_COMPILER ${GCC_ROOT}/bin/g++)
    target_link_libraries(main ${GCC_ROOT}/lib64/libstdc++.so)
    link_directories(${GCC_ROOT}/lib64)
endif(GCC_ROOT)

This script can be used with the usual combo

cmake ..
make

in a 'build' subdirectory. But it is also possible to choose our own compilers by providing the path to the root of our GCC distribution:

cmake -D GCC_ROOT=/home/aleph/gcc/5.2.0 ..
make

The script is written so that the version of libstdc++ corresponds to the version of the input GCC distribution.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use a recent GCC with Xcode 5?

From Dev

Qt5 for Android: incompatible ABI

From Dev

Laravel 5 - How do I handle MethodNotAllowedHttpException

From Dev

How does GCC handle variable redefinition

From Dev

How to handle TokenMismatch exception in laravel 5

From Dev

How does a debugger like GDB handle code optimizations made by compilers like ICC/GCC?

From Dev

How to handle backspace in NSComboBox with swift 5

From Dev

HDF5 how to handle empty rows

From Dev

gcc: how best to handle warning about (unreachable) end of function after switch?

From Dev

std::string & as template parameter and abi_tag in gcc 5

From Dev

How to handle gcc link options(like whole-archive, --allow-multiple-definition) in CMake?

From Dev

How to handle .tar.md5 files

From Dev

How to use a recent GCC with Xcode 5?

From Dev

How do I dual boot Backtrack 5r3 on a PC with Windows 8.1?

From Dev

How to handle type of users in HTML5

From Dev

How do I cross-compile node.js for ARM architecture? GCC does not report the FP ABI compiled for

From Dev

How to handle error compiling GCC 4.7.0 using GCC 6.2.1

From Dev

How should I handle ABI incompatability between gcc-4.9 and gcc-5?

From Dev

How does gcc handle local included files?

From Dev

How to solve: Depends: qtbase-abi-5-11-0 but it is not installable (Debian Sid)

From Dev

How to handle gcc link options(like whole-archive, --allow-multiple-definition) in CMake?

From Dev

Laravel 5 how to handle model's conflicts

From Dev

How to upgrade gcc-5 to gcc-7 in Ubuntu 16.04?

From Dev

How do I compile gcc-5 from source?

From Dev

How to Connect Dual Monitors

From Dev

How to get lambda functions not working on gcc 5+

From Dev

How to detect ABI at compile time?

From Dev

How to install qtbase-abi-5-9-5 in Ubuntu 19.10?

From Dev

If I dual boot Ubuntu 20.04 with Ubuntu 20.04 will grub know how to handle it?

Related Related

  1. 1

    How to use a recent GCC with Xcode 5?

  2. 2

    Qt5 for Android: incompatible ABI

  3. 3

    Laravel 5 - How do I handle MethodNotAllowedHttpException

  4. 4

    How does GCC handle variable redefinition

  5. 5

    How to handle TokenMismatch exception in laravel 5

  6. 6

    How does a debugger like GDB handle code optimizations made by compilers like ICC/GCC?

  7. 7

    How to handle backspace in NSComboBox with swift 5

  8. 8

    HDF5 how to handle empty rows

  9. 9

    gcc: how best to handle warning about (unreachable) end of function after switch?

  10. 10

    std::string & as template parameter and abi_tag in gcc 5

  11. 11

    How to handle gcc link options(like whole-archive, --allow-multiple-definition) in CMake?

  12. 12

    How to handle .tar.md5 files

  13. 13

    How to use a recent GCC with Xcode 5?

  14. 14

    How do I dual boot Backtrack 5r3 on a PC with Windows 8.1?

  15. 15

    How to handle type of users in HTML5

  16. 16

    How do I cross-compile node.js for ARM architecture? GCC does not report the FP ABI compiled for

  17. 17

    How to handle error compiling GCC 4.7.0 using GCC 6.2.1

  18. 18

    How should I handle ABI incompatability between gcc-4.9 and gcc-5?

  19. 19

    How does gcc handle local included files?

  20. 20

    How to solve: Depends: qtbase-abi-5-11-0 but it is not installable (Debian Sid)

  21. 21

    How to handle gcc link options(like whole-archive, --allow-multiple-definition) in CMake?

  22. 22

    Laravel 5 how to handle model's conflicts

  23. 23

    How to upgrade gcc-5 to gcc-7 in Ubuntu 16.04?

  24. 24

    How do I compile gcc-5 from source?

  25. 25

    How to Connect Dual Monitors

  26. 26

    How to get lambda functions not working on gcc 5+

  27. 27

    How to detect ABI at compile time?

  28. 28

    How to install qtbase-abi-5-9-5 in Ubuntu 19.10?

  29. 29

    If I dual boot Ubuntu 20.04 with Ubuntu 20.04 will grub know how to handle it?

HotTag

Archive