Upgrading to G++ 4.8 - exception_ptr.h Does not support exception propagation - c++

I'm trying to recompile a huge legacy app with g++ 4.8 in order debug a glibc detected memory corruption problem (using AddressSanitizer). Previously we used g++ 4.4.7.
However, compilation fails with:
/opt/rh/devtoolset-2/root/usr/include/c++/4.8.2/bits/exception_ptr.h:40:4: error: #error This platform does not support exception propagation.
while compiling a custom exception handler (I guess). The custom exception handler uses exception_ptr in only one place:
void reportOtherException(void) const
{
std::exception_ptr p = std::current_exception();
std::string s = (p != 0 ? p.__cxa_exception_type()->name() : "null");
printf("DvMain Bad Exception: '%s'\n", s.c_str());
mErrorReporter(0, DvLog::WARNING, 0, Dv::NO_PROFILE, 0, DvLog::UNHANDLED_OTHER_EXCEPTION);
}
And reportOtherException() is used like this:
try
{
// Catch and log uncaught exceptions, then exit.
catch (const std::bad_exception& e) { exHandler.reportBadException(e); }
catch (const std::exception& e) { exHandler.reportStandardException(e); }
catch (...) { exHandler.reportOtherException(); }
}
I'm pretty new to C++ and don't know what the error even means. Worked with 4.4.7 and doesn't work with 4.8.
Any pointers on what needs to be changed to compile on 4.8?
EDIT I
Here's some additional info:
g++ --version
g++ (GCC) 4.8.2 20140120 (Red Hat 4.8.2-15)
Minimum Code
DvComDefaultExceptionHandler_test.h
#include "DvCommon.h"
#include "evt/DvEvt.h"
#include "log/DvLog.h"
#include "com/DvComErrorReporter.h"
#include <new>
#include <exception>
#include <sys/time.h>
#include <sys/resource.h>
#include <stdlib.h>
#include <unistd.h>
#include <malloc.h>
#include <bits/exception_ptr.h>
class DvComDefaultExceptionHandler
{
public:
DvComDefaultExceptionHandler(const DvComErrorReporter& er) {}
~DvComDefaultExceptionHandler() { }
void reportOtherException(void) const
{
std::exception_ptr p = std::current_exception();
}
private:
static const DvComDefaultExceptionHandler* mpInstance;
};
DvComDefaultExceptionHandler_test.cpp
#include "DvCommon.h"
#include "com/DvComDefaultExceptionHandler_test.h"
// Pointer to the single instance of the DvComDefaultExceptionHandler class.
const DvComDefaultExceptionHandler*
DvComDefaultExceptionHandler::mpInstance = 0;
Compile command and output
g++ -c -g -O0 -DDEBUG -Wall -Wextra -Wno-sign-compare -Wcast-align
--ftemplate-depth-32 -march=native -ggdb -fPIC -Iinclude -I../../include
-I../../src -I/usr/include/libxml2 -D_GNU_SOURCE
-I/mnt/swdevel/DVMon/source_build/ext/ACE -D__ACE_INLINE__
-I/usr/local/include -I/usr/lib/qt-3.3/include
-o DvComDefaultExceptionHandler.o DvComDefaultExceptionHandler_test.cpp
In file included from ../../include/com/DvComDefaultExceptionHandler_test.h:76:0,
from DvComDefaultExceptionHandler_test.cpp:13:
/opt/rh/devtoolset-2/root/usr/include/c++/4.8.2/bits/exception_ptr.h:40:4: error: #error This platform does not support exception propagation.
# error This platform does not support exception propagation.
EDIT II
Tracing through the include files comes down to the value of __GCC_ATOMIC_INT_LOCK_FREE. Running this simple program prints '2' as the value for __GCC_ATOMIC_INT_LOCK_FREE.
int
main(int argc, char **argv)
{
printf("__GCC_ATOMIC_INT_LOCK_FREE = %d\n", __GCC_ATOMIC_INT_LOCK_FREE);
}
G++ VERSION:
$ g++ --version
g++ (GCC) 4.8.2 20140120 (Red Hat 4.8.2-15)
EDIT II
I've tried it with g++ 6.3.1, running on a Centos 7 VM. Still the same problem.
Source File - one line only
#include <bits/exception_ptr.h>
Compile command: g++ -c -o test.o test.cpp

First of all, including <bits/exception_ptr.h> directly is technically unsupported. It says so right in the header file. This worked in GCC 4.4 by more or less by accident. The C++11 migration of this header file broke it because for namespace reasons, C++98 code cannot use the ATOMIC_INT_LOCK_FREE macro, and the header does not work anymore.
In GCC 7, this was fixed (again accidentally) as part of this bug:
std::future broken on armel
The trick to include <bits/exception_ptr.h> directly should work again in this version.
This means that your options are:
Compile your code in C++11 or later mode (C++14 with GCC 6 recommended).
Upgrade to DTS 7 with GCC 7 if and when it becomes available, which has the upstream fix which re-enables the C++98 hack.
Wrap the use of std::exception_ptr in an opaque type, compile its implementation in C++11 or later mode, and keep the rest of the system in C++98 mode.
Use another hack, perhaps like this one:
#include <exception>
#ifndef ATOMIC_INT_LOCK_FREE
# define ATOMIC_INT_LOCK_FREE __GCC_ATOMIC_INT_LOCK_FREE
#endif
#include <bits/exception_ptr.h>
Again, this is completely unsupported, but it should not be any worse than what you have today (with GCC 4.4).

I was able to reproduce your issue using dockerized Centos6, with gcc 4.8.2. After upgrading dev tools to version 6 (gcc 6.3.1), your code compiled without any problem. Try upgrading dev tools using these steps (suggested for testing only):
add sclo centos6 repository via adding file /etc/yum.repos.d/devtools-sclo.repo :
[testing-devtools]
name=devtools multiple for CentOS
baseurl=http://mirror.centos.org/centos/6/sclo/x86_64/rh/
gpgcheck=0
install devtoolset-6 packages:
yum install devtoolset-6-binutils devtoolset-6-gcc-c++
set bash environment to new version:
scl enable devtoolset-6 bash
Now try recompiling your base example and full source.
NOTE: This same repository contains packages for devtoolset-3 and devtoolset-4 also. Very easy to try if ever needed.

Related

Need help compiling with g++ if the code have threading functionalities using std::thread [duplicate]

I'm trying to compile a simple c++ program that uses std::thread on eclipse kepler / mingw 4.8.1 and win32. I hope to move development to linux at some point after many years on windows development.
#include "test.h"
#include <thread>
#include <algorithm>
int main()
{
Test::CreateInstance();
std::thread(
[&]()
{
Test::I()->Output2();
}
);
Test::DestroyInstance();
return 0;
}
Ignoring the purpose of test (it's a singleton that just produces some output that I will expand upon, once I get the std::thread working!)
The g++ compiler settings I've set in Eclipse are:
-c -fmessage-length=0 -std=c++0x -Wc++0x-compat
And the preprocessor symbol I have defined is:
__GXX_EXPERIMENTAL_CXX0X__
Building complains that std::thread is not a member of std:
10:30:13 **** Incremental Build of configuration Debug for project test ****
Info: Internal Builder is used for build
g++ -D__GXX_EXPERIMENTAL_CXX0X__ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++0x -Wc++0x-compat -o main.o "..\\main.cpp"
..\main.cpp: In function 'int main()':
..\main.cpp:11:2: error: 'thread' is not a member of 'std'
std::thread(
^
Can someone suggest what I might be missing to get this compiling correctly?
Plain MinGW cannot support std::thread. You will need to use a MinGW-w64 toolchain (such as those shipped with Qt 5) that has "posix" threading enabled, so that libstdc++ exposes the <thread>, <mutex> and <future> functionality.
You can find an installer here, but you can also try just replacing the whole mingw toolchain root folder with one of these packages. You can choose 32- or 64-bit, remember to select threads-posix if you want to play with std::thread and friends. No special compiler options other than the ones you already have are needed. I do suggest using -std=c++11 if you don't need GCC 4.6 compatibility.
I had the same problem, though I worked with Cygwin compiler instead. What I did was to define the symbol __cplusplus with the value 201103L for the preprocessor. Also I'd used the flag -std=c++11 for the compiler. This settings have to be made for All configurations (Debug & Release).
Another thing that you may check is that you have properly installed the compiler, verify your compiler instalation as explained by rubenvb.
See here for a native implementation that can be added to any C++11 version of MinGW:
https://github.com/meganz/mingw-std-threads
It is a header-only library, so you just need to include the headers in your project and you will get C++11 threads and synchronization primitives.

Compile error: 'stoi' is not a member of 'std'

My code:
#include <iostream>
#include <string>
int main()
{
std::string test = "45";
int myint = std::stoi(test);
std::cout << myint << '\n';
}
Gives me the compile error:
error: 'stoi' is not a member of 'std'
int myint = std::stoi(test);
^
However, according to here, this code should compile fine. I am using the line set(CMAKE_CXX_FLAGS "-std=c++11 -O3") in my CMakeLists.txt file.
Why is it not compiling?
Update: I am using gcc, and running gcc --version prints out:
gcc (Ubuntu 5.2.1-22ubuntu2) 5.2.1 20151010
In libstdc++, the definitions of stoi, stol, etc., as well as the to_string functions, are guarded by the condition
#if ((__cplusplus >= 201103L) && defined(_GLIBCXX_USE_C99) \
&& !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF))
I have had this fail on one platform before (namely Termux on Android), resulting in to_string not being available even with g++ 6.1 and the C++14 standard. In that case, I just did
#define _GLIBCXX_USE_C99 1
before including anything, and voilà, suddenly the functions existed. (You should put this first, or even on the command line, rather than just before including <string>, because another header may include <string> first, and then its include guards will keep it from ever seeing your macro.)
I did not investigate why this macro wasn't set in the first place. Obviously this is a cause for concern if you want your code to actually work (in my case I didn't particularly, but FWIW there were no problems.)
You should check if _GLIBCXX_USE_C99 is not defined, or if _GLIBCXX_HAVE_BROKEN_VSWPRINTF is defined (which may be the case on MinGW?)
std::stoi is a C++11 function. You have to use the -std=c++11 to enable it in both g++ and clang++. This is the actual issue, not a linking error or a specific preprocessor define.
$ cat test.cxx
#include <iostream>
#include <string>
int main()
{
std::string test = "45";
int myint = std::stoi(test);
std::cout << myint << '\n';
}
$ g++ -otest test.cxx
test.cxx: In Funktion »int main()«:
test.cxx:7:17: Fehler: »stoi« ist kein Element von »std«
int myint = std::stoi(test);
^
$ g++ -otest test.cxx -std=c++11
$ ./test
45
$
edit: I just saw that you used c++11. Are you sure that's making it into your compile options? Check the generated makefile and watch the executed commands to be certain.
Your version seems up to date, so there shouldn't be an issue. I think it may be related to gcc. Try g++ instead.(Most likely automatically linking issue. If you just run gcc on a C++ file, it will not 'just work' like g++ does. That's because it won't automatically link to the C++ std library, etc.). My second advise is try std::atoi.
# I have fixed the issue. std::stoi uses libstdc++. It is about The GNU Standard C++ Library. In gcc you have to link adding -lstdc++. However, in g++, libstdc++ is linked automatically.
using gcc and using g++
Pay attention how it is compiled
using g++: g++ -std=c++11 -O3 -Wall -pedantic main.cpp && ./a.out
using gcc: gcc -std=c++11 -O3 -Wall -pedantic -lstdc++ main.cpp && ./a.out
I think you should set flag like set(CMAKE_EXE_LINKER_FLAGS "-libgcc -lstdc++") (Not tested)
#include <cstdlib>
int myInt = std::atoi(test.c_str());
If you are using Cmake to compile, add line:
"add_definitions(-std=c++11)"
after find_package command.
Use 'set(CMAKE_CXX_STANDARD 11)' for Cmake

Linking silicon webserver with libmicrohttpd backend

I'm trying to compile the silicon webserver hello world example on FreeBSD 10.2 RELEASE using clang++38. The framework uses c++14. I have installed libmicrohttpd.
When I try to compile the program using
clang++38 -O2 -Wall -std=c++14 -I/usr/local/include -L/usr/local/lib -lmicrohttpd -o sws01 sws01.cpp
I get the error
In file included from sws01.cpp:2:
/usr/local/include/silicon/backends/mhd.hh:158:22: error: use of undeclared identifier 'MHD_http_unescape'
value.resize(MHD_http_unescape(&value[0]));
The sws01.cpp:
#include <silicon/api.hh>
#include <silicon/backends/mhd.hh>
#include "symbols.hpp"
using namespace sl;
using namespace s;
auto hello_api = http_api(
GET / _hello = [](){ return D(_message = "Hello from Silicon Webserver!"); }
);
int main() {
sl::mhd_json_serve(hello_api, 9876);
}
I tried to apply this SO thread answer but -Wl and specifying /usr/local/lib/libmicrohttpd.a like
clang++38 -O2 -Wall -std=c++14 -I/usr/local/include /usr/local/lib/libmicrohttpd.a -o sws01 sws01.cpp
did not work either.
Works on os x using xcode 7.3.
Turns out the libmicrohttpd-server that ships with the FreeBSD ports-system is ver. 0.9.37 and libmicrohttpd.so does not have MHD_http_unescape() but libmicrohttpd.a does. The newest is currently 0.9.48. Replacing the ports-version with this solves my compilation issue.

Thread was not declared in this scope error [duplicate]

I'm trying to compile a simple c++ program that uses std::thread on eclipse kepler / mingw 4.8.1 and win32. I hope to move development to linux at some point after many years on windows development.
#include "test.h"
#include <thread>
#include <algorithm>
int main()
{
Test::CreateInstance();
std::thread(
[&]()
{
Test::I()->Output2();
}
);
Test::DestroyInstance();
return 0;
}
Ignoring the purpose of test (it's a singleton that just produces some output that I will expand upon, once I get the std::thread working!)
The g++ compiler settings I've set in Eclipse are:
-c -fmessage-length=0 -std=c++0x -Wc++0x-compat
And the preprocessor symbol I have defined is:
__GXX_EXPERIMENTAL_CXX0X__
Building complains that std::thread is not a member of std:
10:30:13 **** Incremental Build of configuration Debug for project test ****
Info: Internal Builder is used for build
g++ -D__GXX_EXPERIMENTAL_CXX0X__ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++0x -Wc++0x-compat -o main.o "..\\main.cpp"
..\main.cpp: In function 'int main()':
..\main.cpp:11:2: error: 'thread' is not a member of 'std'
std::thread(
^
Can someone suggest what I might be missing to get this compiling correctly?
Plain MinGW cannot support std::thread. You will need to use a MinGW-w64 toolchain (such as those shipped with Qt 5) that has "posix" threading enabled, so that libstdc++ exposes the <thread>, <mutex> and <future> functionality.
You can find an installer here, but you can also try just replacing the whole mingw toolchain root folder with one of these packages. You can choose 32- or 64-bit, remember to select threads-posix if you want to play with std::thread and friends. No special compiler options other than the ones you already have are needed. I do suggest using -std=c++11 if you don't need GCC 4.6 compatibility.
I had the same problem, though I worked with Cygwin compiler instead. What I did was to define the symbol __cplusplus with the value 201103L for the preprocessor. Also I'd used the flag -std=c++11 for the compiler. This settings have to be made for All configurations (Debug & Release).
Another thing that you may check is that you have properly installed the compiler, verify your compiler instalation as explained by rubenvb.
See here for a native implementation that can be added to any C++11 version of MinGW:
https://github.com/meganz/mingw-std-threads
It is a header-only library, so you just need to include the headers in your project and you will get C++11 threads and synchronization primitives.

std::thread is not a member of namespace std using Eclipse Kepler MinGW

I'm trying to compile a simple c++ program that uses std::thread on eclipse kepler / mingw 4.8.1 and win32. I hope to move development to linux at some point after many years on windows development.
#include "test.h"
#include <thread>
#include <algorithm>
int main()
{
Test::CreateInstance();
std::thread(
[&]()
{
Test::I()->Output2();
}
);
Test::DestroyInstance();
return 0;
}
Ignoring the purpose of test (it's a singleton that just produces some output that I will expand upon, once I get the std::thread working!)
The g++ compiler settings I've set in Eclipse are:
-c -fmessage-length=0 -std=c++0x -Wc++0x-compat
And the preprocessor symbol I have defined is:
__GXX_EXPERIMENTAL_CXX0X__
Building complains that std::thread is not a member of std:
10:30:13 **** Incremental Build of configuration Debug for project test ****
Info: Internal Builder is used for build
g++ -D__GXX_EXPERIMENTAL_CXX0X__ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++0x -Wc++0x-compat -o main.o "..\\main.cpp"
..\main.cpp: In function 'int main()':
..\main.cpp:11:2: error: 'thread' is not a member of 'std'
std::thread(
^
Can someone suggest what I might be missing to get this compiling correctly?
Plain MinGW cannot support std::thread. You will need to use a MinGW-w64 toolchain (such as those shipped with Qt 5) that has "posix" threading enabled, so that libstdc++ exposes the <thread>, <mutex> and <future> functionality.
You can find an installer here, but you can also try just replacing the whole mingw toolchain root folder with one of these packages. You can choose 32- or 64-bit, remember to select threads-posix if you want to play with std::thread and friends. No special compiler options other than the ones you already have are needed. I do suggest using -std=c++11 if you don't need GCC 4.6 compatibility.
I had the same problem, though I worked with Cygwin compiler instead. What I did was to define the symbol __cplusplus with the value 201103L for the preprocessor. Also I'd used the flag -std=c++11 for the compiler. This settings have to be made for All configurations (Debug & Release).
Another thing that you may check is that you have properly installed the compiler, verify your compiler instalation as explained by rubenvb.
See here for a native implementation that can be added to any C++11 version of MinGW:
https://github.com/meganz/mingw-std-threads
It is a header-only library, so you just need to include the headers in your project and you will get C++11 threads and synchronization primitives.