What is the "Illegal Instruction: 4" error and why does "-mmacosx-version-min=10.x" fix it?

MacosGccOsx LionOsx Mountain-LionOsx Leopard

Macos Problem Overview


I get Illegal Instruction: 4 errors with binaries compiled with GCC 4.7.2 under Mac OS X 10.8.2 ("Mountain Lion"), when those binaries are run under Mac OS X 10.7.x ("Lion") and earlier versions. The binaries work properly under Mac OS X 10.8.x.

I added -mmacosx-version-min=10.5 to my compile flags and this seems to help resolve the issue for 10.5.x, 10.6.x and 10.7.x clients, whatever that issue is.

Which gets to my question(s):

  • What is the Illegal Instruction: 4 error?
  • Why does -mmacosx-version-min=10.x fix this specific error on 10.x and greater clients?

I'd like to apply this fix to my makefiles, but would like to know what it is doing before I pull the trigger. (Will I have larger binaries? Do I still have 64-bit binaries? Are there gotchas with this approach I should know about? Unintended side-effects? Etc.)

Macos Solutions


Solution 1 - Macos

From the Apple Developer Forum (account required):

> "The compiler and linker are capable of using features and performing optimizations that do not work on older OS versions. -mmacosx-version-min tells the tools what OS versions you need to work with, so the tools can disable optimizations that won't run on those OS versions. If you need to run on older OS versions then you must use this flag.

> "The downside to -mmacosx-version-min is that the app's performance may be worse on newer OS versions then it could have been if it did not need to be backwards-compatible. In most cases the differences are small."

Solution 2 - Macos

The "illegal instruction" message is simply telling you that your binaries contain instructions the version of the OS that you are attempting to run them under does not understand. I can't give you the precise meaning of 4 but I expect that is internal to Apple.

Otherwise take a look at these... they are a little old, but probably tell you what you need to know

https://stackoverflow.com/questions/2482285/how-does-64-bit-code-work-on-os-x-10-5?rq=1
https://stackoverflow.com/questions/2923502/what-does-macosx-version-min-imply

Solution 3 - Macos

I'm consciously writing this answer to an old question with this in mind, because the other answers didn't help me.

I got the Illegal Instruction: 4 while running the binary on the same system I had compiled it on, so -mmacosx-version-min didn't help.

I was using gcc in Code Blocks 16 on Mac OS X 10.11.

However, turning off all of Code Blocks' compiler flags for optimization worked. So look at all the flags Code Blocks set (right-click on the Project -> "Build Properties") and turn off all the flags you are sure you don't need, especially -s and the -O flags for optimization. That did it for me.

Solution 4 - Macos

I found my issue was an improper
if (leaf = NULL) {...}
where it should have been
if (leaf == NULL){...}

Check those compiler warnings!

Solution 5 - Macos

I got this error when attempting to build with Xcode 10. It appears to be a bug in the Swift compiler. Building with Whole Module Optimization on, resolves the issue: https://forums.swift.org/t/illegal-instruction-4-when-trying-to-compile-project/16118

This is not an ideal solution, I will continue to use Xcode 9.4.1 until this issue is resolved.

Solution 6 - Macos

In my case, I got this while overloading

ostream & operator << (ostream &out, const MyClass &obj)

and forgot to return out. In other systems this just generates a warning, but on macos it also generated an error (although it seems to print correctly).

The error was resolved by adding the correct return value. In my case, adding the -mmacosx-version-min flag had no effect.

Solution 7 - Macos

I recently got this error. I had compiled the binary with -O3. Google told me that this means "illegal opcode", which seemed fishy to me. I then turned off all optimizations and reran. Now the error transformed to a segfault. Hence by setting -g and running valgrind I tracked the source down and fixed it. Reenabling all optimizations showed no further appearances of illegal instruction 4.

Apparently, optimizing wrong code can yield weird results.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlex ReynoldsView Question on Stackoverflow
Solution 1 - MacosAlex ReynoldsView Answer on Stackoverflow
Solution 2 - MacosfoundryView Answer on Stackoverflow
Solution 3 - Macoscurious_weatherView Answer on Stackoverflow
Solution 4 - MacosJaguilarView Answer on Stackoverflow
Solution 5 - MacosKyle RedfearnView Answer on Stackoverflow
Solution 6 - Macosblue_noteView Answer on Stackoverflow
Solution 7 - MacosPascal EngelerView Answer on Stackoverflow