About compiling C++98 codes using C++11 standard - c++

What are additional configurations required in compilations and linking to successfully compile the c++98 codes using C++11/gnu+11 standard?
Existing gcc version is g++ (GCC) 4.8.5 20150623 on RHEL7 OS.
In order to use C++11 standard on the existing code written in C++98, flag -std=c++11 was added along with -D_GLIBCXX_USE_CXX11_ABI=0.
While compiling existing code, the following error is still thrown:
undefined reference to `__cxa_throw_bad_array_new_length'
collect2: error: ld returned 1 exit status
Please, help on this issue.

The post program linking fails when using custom built gcc helped me resolve the issue.
It was due to linking of the object file with different version of the system libraries.

Related

How to compile with clang and mingw-w64?

I'm trying to use clang10 with mingw-w64's libstdc++, since the MSVC headers don't support clang 10. I don't mind not having the new parts of the standard library, I just want to use the new language features.
I have mingw-w64 version 8.1.0 for x86_64 with POSIX threads and SEH exceptions installed and I run clang with a command:
clang++ -target x86_64-pc-windows-gnu -std=c++20 ...
Everything works OK. All the language features that should be implemented in clang 10 work, but when I throw any exception, this happens when I run the program (the program compiles OK):
Mingw-w64 runtime failure:
Unknown pseudo relocation protocol version 65536.
I tried installing mingw with SJLJ exceptions and use the -fsjlj-exceptions flag in clang, but the program doesn't even compile:
C:\Users\egst\AppData\Local\Temp\test-f4a4de.o:test.cpp:(.text+0x82): undefined reference to `__gxx_personality_sj0'
C:\Users\egst\AppData\Local\Temp\test-f4a4de.o:test.cpp:(.text+0xd9): undefined reference to `_Unwind_SjLj_Register'
C:\Users\egst\AppData\Local\Temp\test-f4a4de.o:test.cpp:(.text+0x177): undefined reference to `_Unwind_SjLj_Resume'
clang++ --version shows Target: x86_64-pc-windows-msvc. Maybe there's a version for x86_64-pc-windows-gnu that I should use instead? Is there any way to make this work at this moment, or should I wait for support from MSVC? Is there maybe any alternative besides MSVC and MinGW?
Based on #HolyBlackCat 's sugestion, I tried this with MSYS2 and it works perfectly. I simply installed MSYS2, then from the MSYS bash I installed this package. It installs everything in in ...\msys2\mingw64. You get clang 10 (+ gcc 9.3) and all the needed stl headers (still no c++20 headers like <concepts> though).

gcc build behavior when there is are conflicting version of openssl

I am trying to build a c++ library of mine (which is dependent on facebook/folly) with gcc7. Some important points to note
Previously I was using gcc 4.8 to build this library
Since there is ABI incompatibility introduced in gcc 5.2 - I built folly from source (and all other dependent libraries like glog etc.) with gcc 7 - and I am using the updated libraries and shared objects.
My library uses openssl 1.1 (even previously when it was being built with gcc 4.8), where as folly (the version of folly that I am using) uses openssl 1.0.2
Even with the openssl version incompatibility the build of my library would go through fine with gcc 4.8 with the following warning
/usr/bin/ld: warning: libssl.so.10, needed by /home/<username>/folly/folly/1000.37.2.1/lib/libfolly.so, may conflict with libssl.so.1.1
I get the exact same warning with gcc 7 as well, but my build now fails with the following error
/usr/bin/ld: /home/<username>/folly/folly/1000.37.2.1/lib/libfolly.a(SSLSessionCacheManager.o): undefined reference to symbol 'SSL_CTX_get_ex_new_index##libssl.so.10'
/usr/lib64/libssl.so.10: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
I have tried researching this problem in regards to DSO missing from command line - but the solutions mentioned in other questions doesn't seem to be my case.
There are 3 questions that I am seeking to answer here
Can openssl version incompatibility be the reason for the build failure?
Why would the build succeed in gcc 4.8 but fail on gcc 7?
Is there an way to solve this without updating openssl version in folly (I will have to pull in a lot of changes for that - which will require code changes from my side). My library only uses openssl/md5.h

MinGW-w64's gcc and Address Sanitizer

Installing MinGW-w64 5.1 I find -fsanitize=address is available. It compiles fine, and when it starts linking I get thousands of:
undefined reference to '__asan_report_load1'
undefined reference to '__asan_report_load4'
I googled and found libasan referenced various places, but also comments that when you include -fsanitize=address it automatically includes that library for linking. I searched the MinGW-w64 5.1 install dirctory for "asan" and it was not found anywhere.
What do I need to add on to use address sanitizing features in MinGW-w64? Thank you.
I've looked quickly into release notes for 4.8, 4.9 and 5.1 - ASAN is not available for Windows

undefined references during link phase using Boost 1.53

I am using g++ 4.1.2 and Boost 1.53 on Red Hat Linux rel 5.5. There is an issue during the linking process which is unclear to me. I get the error msg:
/usr/local/boost_1_53_0/stage/lib/libboost_thread.so:
undefined reference to `std::bad_exception::what() const#GLIBCXX_3.4.9'
/usr/local/boost_1_53_0/stage/lib/libboost_thread.so:
undefined reference to `std::bad_alloc::what() const#GLIBCXX_3.4.9'
collect2: ld returned 1 exit status
There seems to be a problem related to the libstdc++.so file version I'm linking with. If I alternate hiding and revealing this file I can get my program to link. However, I would like to be able to 'make' in one step as I did before I introduced the Boost libraries and concurrent threading.
Please help shed some light on this issue if you have any insight.
Please try to install via yum.
yum install boost
You will not encouter such issues.
Try to include rpmforge and epel yum repositories.
http://gcc.gnu.org/onlinedocs/libstdc++/manual/abi.html#abi.versioning shows that GLIBCXX_3.4.9 is the symbol version used by the libstdc++.so from GCC 4.2.0, so if the Boost library wants that version then it means it was compiled with GCC 4.2.x
That means you can't use the library with a program linked by GCC 4.1.2, you need to link with GCC 4.2.0 or later
libstdc++.so is backward compatible but not forwards compatible, so you can compile a program with one version and link it to a newer version, but you can't compile with a newer version and link to an older version.

How to use std::thread of C++ 11 under Cygwin GCC 4.7.2

I've been trying to compile a multithread hello-world program under Cygwin using the newly introduced C++ 11 std::thread feature without success. I compiled and installed GCC 4.7.2 by myself, and the same code works without any problems under Linux with the same version of GCC. The first error I got was that the compiler did not recognize the -pthread flag. After researching on it for a while I noticed someone said on Cygwin this flag should be -lthread. I made the change and that error was gone, but another series of errors occur telling me thread is not member of std. I wonder if it's caused by the wrong configuration of the compiler during installation, or std::thread is simply not supported under Cygwin?
This looks like you did not compile the program with the appropriate standard library flag. If you want to compile for C++11 you should use:
g++ --std=c++0x -o ...
The --std flag sets the appropriate language compatibility level. If this does not help, please post the error messages you got as a source listing.