I'm just trying a few of the new C++11 features with GCC 4.7.2, though when I go to run a seg fault occurs.
$ ./a.out
Message from main.
terminate called after throwing an instance of 'std::system_error'
what(): Unknown error -1
Aborted (core dumped)
I compiled with the 'beta' features of GCC, in regards to c++0x with:
g++ -std=c++11 c11.cpp
The code:
#include <future>
#include <iostream>
void called_from_async() {
std::cout << "Async call" << std::endl;
}
int main() {
//called_from_async launched in a separate thread if possible
std::future<void> result( std::async(called_from_async));
std::cout << "Message from main." << std::endl;
//ensure that called_from_async is launched synchronously
//if it wasn't already launched
result.get();
return 0;
}
I believe this happens because you have forgot to link with POSIX threads library. Just add -pthread or -lpthread to the g++ flags and the problem should go away.
If you are interested in details, this happens because C++11 runtime is resolving symbols from pthread in run-time only if you happen to use those features. So if you forgot to link, the runtime won't be able to resolve those symbols, treat your environment as if it doesn't support threads, and throw exception (which you don't catch and it aborts your application).
Related
I keep getting terminate called for anything I throw using GCC 9.2, even if it is caught.
terminate called after throwing an instance of 'char const*'
terminate called recursively
I have tested -std=c++17, -std=c++14, -std=c++11
Example test:
#include <iostream>
int main()
{
try
{
throw "not found";
}
catch(...)
{
std::cout << "test" << std::endl;
}
return 0;
}
It doesn't fail if i compile using visual studio or on multiple of the online compilers.
example:
https://repl.it/languages/cpp
https://www.onlinegdb.com/online_c++_compiler
I have also tried putting the throw in a function and adding noexcept(false), but this fails as well. Example:
#include <iostream>
void foo() noexcept(false)
{
throw std::runtime_error( "test1" );
}
int main()
{
try
{
foo();
}
catch(...)
{
std::cout << "test2" << std::endl;
}
return 0;
}
Edit:
System info:
I'm using 9-2020-q2-update - arm-linux-none-gnueabihf.
Basically, the setup is Linux x86 as my main computer, cross compiling for ARM Cortex-A processor. The processor i'm testing are Raspberry Pi 4 and BeagleBone Black.
The code compiles correctly and runs fine on the target processor, except when an exception is hit. At which point, it hits terminate for any throw.
I'm using Eclipse as the IDE, using remote debug to upload and step through the code on either of the target processors.
It seems there is a bug or exception handling isn't working on version 9.2 of GCC (ARM only?) compiler.
I tried with version 8.3-2019.03 - arm-linux-gnueabihf - Linux x86 compiler and they are working just fine. No other changes were necessary, other than the compile switch.
https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-a/downloads
I am trying to compile my C++ Raspberry Pi 3 code through gcc with a -march=armv8-a flag. However, using this flag causes my threads to fail by saying pure virtual method called. I know it is the -march=armv8-a flag because when I compile it without it, the threads start working again.
Please note: The thread does not even start, it just gives a pure virtual method called
Can someone compile this with -march=armv8-a on their Raspberry Pi 3 and report back on what they got?
#include <iostream>
#include <thread>
#include <unistd.h>
void threadedFunction()
{
std::cout << "Hello from thread" << std::endl;
}
int main()
{
std::thread t1(threadedFunction);
sleep(2);
return 0;
}
Since armv8-a uses a 64-bit architecture, using the -march=armv8-a flag is going to compile for a 64-bit machine. However, many Raspberry Pi OS(images) are 32-bit which may cause crashes or errors.
Credit: https://stackoverflow.com/users/1505939/m-m
I'm using C++ Primer 5th to learn C++. The code below is copied from P729.
#include <iostream>
#include <regex>
#include <string>
int main()
{
// find the characters ei that follow a character other than c
std::string pattern("[^c]ei");
pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";
std::regex r(pattern);
// ~~~~~~~~~~~^~~~~~~~~~~ where the exception was thrown.
std::smatch results;
std::string test_str = "receipt freind theif receive";
if (regex_search(test_str, results, r))
std::cout << results.str() << std::endl;
return 0;
}
When running it, an exception was thrown :
terminate called after throwing an instance of 'std::regex_error'
what(): regex_error
Press <RETURN> to close this window...
By debugging step by step, I found it was thrown while constructing the object r from the code from bits/regex_compiler.h:
template<typename _InIter, typename _TraitsT>
bool
_Compiler<_InIter, _TraitsT>::
_M_bracket_expression()
{
if (_M_match_token(_ScannerT::_S_token_bracket_begin))
{
_RMatcherT __matcher(_M_match_token(_ScannerT::_S_token_line_begin),
_M_traits);
if (!_M_bracket_list(__matcher)
|| !_M_match_token(_ScannerT::_S_token_bracket_end))
__throw_regex_error(regex_constants::error_brack);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_M_stack.push(_StateSeq(_M_state_store,
_M_state_store._M_insert_matcher(__matcher)));
return true;
}
return false;
}
OK.The code above is totally beyond what I can understand.That's what I have tried so far.Can anyone tell me what's gong on here? How to fix it?
UPDATE:
The compiler I'm using:
gcc version 4.8.1 (Ubuntu 4.8.1-2ubuntu1~13.04)
You need to use minimal version of g++ 4.9 to use regex properly.(You can compile it with lower versions, but it's broken)
You can download g++ 4.9 from their side. GCC SITE
After that try:
g++49 -std=c++0x -static-libstdc++.
I try to search more about it for you.
EDIT: g++49, because of the reason, that version 4.9 can't be default after building it.
#include <iostream>
#include <thread>
int main()
{
std::thread th([] { std::cout << "Hello, World\n"; });
th.join();
}
This is all I have and it causes a runtime error. Why is that? I'm using GCC 4.8 (Ideone).
The error from ideone is:
terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not permitted
Which means you need to be compiling with -pthread as already suggested by #Praetorian.
The code runs fine in Visual Studio 2012.
Does the stack get unwound (destructors run) when a SIGABRT occurs in C++?
Thanks.
No:
$ cat test.cc
#include <iostream>
#include <sys/types.h>
#include <signal.h>
class Test {
public:
~Test() { std::cout << "~Test called" << std::endl; }
};
int main(int argc, char *argv[])
{
Test t = Test();
if (argc > 1) {
kill(0, SIGABRT);
}
return 0;
}
$ g++ test.cc
$ ./a.out
~Test called
$ ./a.out 1
Aborted
This answer indicates that destructors aren't called.
No, only exceptions trigger stack unwinding. Signals are part of POSIX, which is a C API, so it's not "aware of" C++ facilities such as exceptions.
The signal(3) man page on my Mac OS X box says
No Name Default Action Description
...
6 SIGABRT create core image abort program (formerly SIGIOT)
which suggests to me that the default is to not unwind...
the signal SIGABRT is used for making core file of running application some time. We some time use this signal to debug application. And as far as I know Destructors are not called by this signal.