Zeldhoron
Zeldhoron

Reputation: 69

GCC illegal instruction

When I compile this code:

#include <random>
#include <iostream>

int main(int argc, char** argv)
{
    std::random_device dev;
    std::mt19937 mt(dev());
    std::cout << mt() << std::endl;
    return 0;
}

And then try to run the resulting executable with gdb I get this error:

Program received signal SIGILL, Illegal instruction. std::(anonymous namespace)::__x86_rdrand () at /build/gcc/src/gcc/libstdc++-v3/src/c++11/random.cc:69 69 /build/gcc/src/gcc/libstdc++-v3/src/c++11/random.cc: No such file or directory.

I use arch linux with a Intel Core 2 Duo CPU T8100. How do I fix this?

Upvotes: 2

Views: 3695

Answers (1)

Peter
Peter

Reputation: 14937

The error message is "Illegal instruction", and the only hint you get is __x86_rdrand(). Googling rdrand leads to the RDRAND instruction, which appears to have been added for Ivy Bridge Processors, which your Core 2 Duo most certainly is not. (It's a Penryn on this chart: https://en.wikipedia.org/wiki/Template:Intel_processor_roadmap)

OK, so your CPU doesn't have RDRAND. That means the compiler must have the wrong information about what it's target is. With GCC, the flag to adjust is -march. In your case, -march=core2 should do it. It should also be ok to say -march=native, which will target exactly what you're compiling on.

Upvotes: 6

Related Questions