什么是“非法指令: 4”错误,为什么“-mmacosx-version-min = 10.x”修复它?

在 Mac OS X 10.8.2(“ Mountain Lion”)下用 GCC 4.7.2编译的二进制文件在 Mac OS X 10.7. x (“ Lion”)和早期版本下运行时,我会遇到 Illegal Instruction: 4错误。二进制文件在 Mac OS X 10.8. x 下正常工作。

我将 -mmacosx-version-min=10.5添加到我的编译标志中,这似乎有助于解决10.5. x、10.6. x 和10.7. x 客户端的问题,不管这个问题是什么。

这就引出了我的问题:

  • 什么是 Illegal Instruction: 4错误?
  • 为什么 -mmacosx-version-min=10.x10.x和更大的客户端上修复这个特定的错误?

我想应用这个修复到我的 makefile,但想知道它在做什么之前,我扣动扳机。(我会有更大的二进制文件吗?我还有64位二进制文件吗?这种方法有什么我应该知道的陷阱吗?意外副作用?等等)

181673 次浏览

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?

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

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.

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

Check those compiler warnings!

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.

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.

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.