91

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.)

5
  • Snow Leopard is Mac OSX 10.6, please clarify/correct
    – foundry
    Commented Jan 10, 2013 at 23:07
  • 4
    A missing return value may cause an "Illegal Instruction: 4". I ran into this yesterday. Paying attention to my compiler warnings brought me on the right track.
    – rsp1984
    Commented Apr 20, 2014 at 16:26
  • 1
    I asked this question seven years ago and would be unable to help you today, sorry. I do recall trying to distribute binaries for older versions of Mac OS X, though that seems like a fools game these days. Commented Jul 19, 2020 at 5:07
  • I'm getting this error too, and so far none of the suggestions here helped. I policed-up my compiler warnings (they're totally gone now), tried -mmacosx-version-min (it was an unrecognized option), tried both clang and gcc (same result), and tried compiling without -O. It's not "just" the program I'm compiling either: Outlook for MacOS has the same problem. Any further suggestions?
    – dstromberg
    Commented Feb 28, 2022 at 22:43
  • I'm sorry, I wish I could help! Commented Feb 28, 2022 at 23:02

8 Answers 8

44

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."

1
  • 3
    The place to put this flag is under “Other Linker Flags” in the project’s build settings, e.g. -mmacosx-version-min=10.10.
    – Demitri
    Commented May 10, 2017 at 14:52
25

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

How does 64 bit code work on OS-X 10.5?
what does macosx-version-min imply?

3
  • Thanks; those two questions and associated answers are interesting, and I read them before asking, but I ask this question because neither particularly addressed this error, its cause and why the use of this flag is a solution. Before I put this into production, I just want to make sure that I'm not breaking something for another set of users, say, and it's unclear to me if there are any side effects. Commented Jan 10, 2013 at 23:22
  • 3
    I would suggest that you shouldn't worry, you are going with the intention of the system designers. There's a nice quote from Elements of Programming Style I came across today... "Trying to outsmart a compiler defeats much of the purpose of using one"
    – foundry
    Commented Jan 11, 2013 at 7:30
  • @foundry An old quote and I seem to think it was even when you wrote that. I would agree except when you're writing code for the iOCCC! :) But yes people do tend to not want to deal with warnings etc. What always baffles me is if they don't want to fix a warning why do they have warnings enabled at all? If it's one that is always enabled and there's no way to disable it then fix the ruddy code and be done with it. Or else ignore it. But warnings are a developer's friend. I've seen this illegal instruction 4 on the same system it's compiled on but under Linux (compiled there) it works fine.
    – Pryftan
    Commented Jan 30, 2020 at 15:42
6

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 -Oflags for optimization. That did it for me.

2
  • 1
    My CLI app works fine in it's original directory but when moved it gives this error. Must be something in my code, exiting the app early and printing a hello world works fine. Commented Sep 17, 2016 at 5:34
  • It should be remembered though that optimisers can find bugs and by disabling them you can be masking a bug. Of course some code really shouldn't be optimised so who can tell? But still it would be better if you could find the source of the bug even if you decide that it's not worth it in the end to fix.
    – Pryftan
    Commented Jan 30, 2020 at 15:48
5

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

Check those compiler warnings!

1
  • 3
    You know what that is jokingly called sometime, the = operator? '`Is leaf NULL? It is now!'
    – Pryftan
    Commented Jan 30, 2020 at 15:43
4

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.

3

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.

2

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.

0

Honestly, I kept getting this and didn't find a good answer. I was getting this error no matter how I used wget. I hit it with a hammer.

This was the fix for me: I removed the binary that came with OSX and used the one that brew installs. After that, no more error.

# Move the current binary to temp so we can restore it if we need later
sudo mv /usr/local/bin/wget /tmp
# Install wget from brew
brew install wget
# Set a link so "wget" on the command line points to the brew install
sudo ln -s /opt/homebrew/bin/wget /usr/local/bin/wget
2
  • I do not understand how this relates to my question. Commented Mar 16 at 5:38
  • My apologies as it is unrelated. I was getting the same error you were when trying to run the command "wget" in OSX.
    – Patrick
    Commented Mar 19 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.