What is the error "Invalid instruction: 4" and why "-mmacosx-version-min = 10.x" fix?

I get Illegal Instruction: 4 errors with binaries compiled with GCC 4.7.2 on Mac OS X 10.8.2 (Mountain Lion) when these binaries run under Mac OS X 10.7.x ("Lion") and earlier version. Binary files work correctly on Mac OS X 10.8.x.

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

Regarding my questions:

  • What is Illegal Instruction: 4 Error?
  • Why -mmacosx-version-min=10.x correct this specific error on 10.x and larger clients?

I would like to apply this fix to my makefiles, but I would like to know what it does before I pull out the trigger. (Do I have large binaries? Do I still have 64-bit binaries? Are there any errors with this approach that I should be aware of? Unintended side effects, etc.)

+57
gcc osx-lion osx-leopard osx-mountain-lion macos
Jan 10
source share
7 answers

From the Apple Developer Forum (account required):

"The compiler and linker are able to use functions and perform optimizations that do not work on older versions of the OS. -mmacosx-version-min tells the tools which OS versions you need to work, so tools can disable optimizations that won't run these OS versions If you need to run older versions of the OS, you should use this flag.

"The disadvantage of -mmacosx-version-min is that application performance may be worse on newer versions of the OS than it would be if it were not required to be backward compatible. In most cases, the differences are not significant."

+35
Jan 11 '13 at 20:12
source share

The “illegal instruction” message simply tells you that your binaries contain instructions that the version of the OS you are trying to run does not understand. I cannot give the exact meaning of 4 , but I expect this to be internal to Apple.

Otherwise, take a look at them ... they are a bit old, but they will probably tell you what you need to know.

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

+18
Jan 10 '13 at 23:18
source share

I deliberately write this answer to an old question with this in mind, because the other answers did not help me.

I got Illegal Instruction: 4 when I -mmacosx-version-min binary on the same system on which I compiled it, so -mmacosx-version-min did not help.

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

However, when you turn off all flags of the Code Blocks compiler for optimization. Look at all the flags set in the Code Block (right-click Project → "Build Properties") and disable all the flags that you absolutely do not need, especially -s and -O flags for optimization. This did it for me.

+3
Jul 06 '16 at 7:45
source share

I found that my problem was wrong if (leaf = NULL) {...}
where it should have been if (leaf == NULL){...}

Check out these compiler warnings!

+2
Jul 07 '17 at 4:29 on
source share

I got this error when trying to build with Xcode 10. It seems to be a bug in the Swift compiler. Whole Module Optimization , solves the problem: https://forums.swift.org/t/illegal-instruction-4-when-try-to-compile-project/16118.

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

+2
05 Oct '18 at 20:25
source share

I recently got this error. I compiled binary with -O3. Google told me that it meant an “illegal transaction code,” which seemed suspicious to me. Then I turned off all optimizations and repeated. Now the error has turned into segfault. Therefore, by setting -g and running valgrind, I tracked the source and committed it. The reuse of all optimizations did not show any further manifestations of the illegal instruction 4.

Apparently optimizing the wrong code can give strange results.

0
Feb 16 '18 at 13:44
source share

In my case, I got this on overload

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

and forgot to come back out . On other systems, this just generates a warning, but on macos it also throws an error (although it seems to print correctly).

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

0
Apr 16 '19 at 12:51
source share



All Articles