編譯策略介紹
關於優化級別:GCC_OPTIMIZATION_LEVEL 描述如下
None: Do not optimize.
[-O0]With this setting, the compiler’s goal is to reduce the cost of compilation and to make debugging produce the expected results. Statements are independent: if you stop the program with a breakpoint between statements, you can then assign a new value to any variable or change the program counter to any other statement in the function and get exactly the results you would expect from the source code.Fast: Optimizing compilation takes somewhat more time, and a lot more memory for a large function.
[-O, -O1]With this setting, the compiler tries to reduce code size and execution time, without performing any optimizations that take a great deal of compilation time. In Apple’s compiler, strict aliasing, block reordering, and inter-block scheduling are disabled by default when optimizing.Faster: The compiler performs nearly all supported optimizations that do not involve a space-speed tradeoff.
[-O2]With this setting, the compiler does not perform loop unrolling or function inlining, or register renaming. As compared to the ‘Fast’ setting, this setting increases both compilation time and the performance of the generated code.Fastest: Turns on all optimizations specified by the ‘Faster’ setting and also turns on function inlining and register renaming options. This setting may result in a larger binary.
[-O3]Fastest, smallest: Optimize for size. This setting enables all ‘Faster’ optimizations that do not typically increase code size. It also performs further optimizations designed to reduce code size.
編譯策略資料:
https://gcc.gnu.org/onlinedocs/gnat_ugn/Optimization-Levels.html
拓展問題
1.今天在測試app時,發現app store上版本有奔潰現象,但是直接用xcode跑本地程序沒問題。猜測release&debug版本造成的,后發現targets的 optimization level設置問題,將release版本的optimization level改為none后程序不再崩潰
2.問答
問:寫了個程序。但在release模式下真機測試 ,不能正常工作。例如界面從網絡上獲取的圖片等等。 但當optimization level設置成NONE后所有問題都正常了。
發布到APPSTORE上的程序編譯時必須設置optimization level為 [-Os] 嗎?如果設置為NONE會不會不通過審核?
答:Optimization Level 應該是編譯器的優化程度。
比較早期的時候,硬件資源是比較缺乏的。為了提高性能,開發編譯器的大師們,都會對編譯器(從c到匯編的編譯過程)加上一定的優化策略。優化后的代碼效率比較高,但是可讀性比較差,且編譯時間更長。
優化是指編譯器一級的措施,與機器指令比較接近,所以很可能會導致硬件不兼容,進而產生了你目前遇到的軟件裝不上的問題。
他是編譯器的行為,與你代碼理論上不相關的。 蘋果的檢查應該是檢查你的代碼一級的規范程度,隱私侵權相關的問題。 應該是與編譯的過程是無關的。請放心。