Raspberry Pi C++11 std::thread: pure virtual method called

Ubuntu 14.04. I've installed gcc-arm-linux-gnueabihf, g++-arm-linux-gnueabihf (tried 4.8 and 4.9 from utopic).

Code that uses std::thread:

#include <iostream>
#include <chrono>
#include <future>
void secondList()
{ const std::chrono::seconds twoSeconds(2); for (size_t i = 0; i != 300; ++i) { std::this_thread::sleep_for(twoSeconds); std::cout << "2s\n"; }
}
int main(int, const char *[])
{ auto secondThr = std::async(std::launch::async, secondList); return 0;
}

Compiling with:

arm-linux-gnueabihf-g++ --std=c++11 main.cpp -lpthread -o main

On RPI it fails:

pi@raspberrypi ~ $ ./main
pure virtual method called
terminate called without an active exception
Aborted

Compiling on RPI works:

pi@raspberrypi ~ $ g++ --std=c++0x main.cpp -lpthread -o main

Pi image 2015-02-16-raspbian-wheezy, g++ on Pi (Debian 4.6.3-14+rpi1) 4.6.3.

I've tried compiler options -mcpu=cortex-a7, -mcpu=cortex-a8 and -D__GCC_HAVE_SYNC_COMPARE_AND_SWAP_{1,2,4,8} that were mentioned in similar questions.

Also tried g++ from ppa: precise

Why it is happening and how to get a cross-compiler that works?

11

3 Answers

With jessie-based image it works.

1

Add this to the command line for linking,

-Wl,--whole-archive -lpthread -Wl,--no-whole-archive

See this message for explanations, It said to be a problem of TLS, but maybe it's related incomplete symbols linked from static libpthread.

It can probably be fixed by adding -D__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1 -D__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2 -D__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8 to the command line flags.

The root cause and fix are described here:

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like