'Project Name' was compiled with optimization - stepping may behave oddly; variables may not be available

不想你离开。 提交于 2019-11-26 05:57:39

问题


Trying to step into AFNetworking code generates following warning:

[Project Name] was compiled with optimization - stepping may behave oddly; variables may not be available.

And of course I\'m not able to debug the code. To be specific I\'m trying to debug UIImageView+AFNetworking category which seems impossible. Changing the code has no effect (tried NSLog, etc) and when trying to step in compilers goes to assembly code and shows UIImageView+TVASTAFNetworking as category name which does not exist anywhere in the code base.

Using Xcode 7. iOS 9 & 8. Cocoapods (no Framework)

UPDATE I forgot to mention that Optimizer is set to none for both release and debug configuration and I am in fact using Debug config.

UPDATE 2

Strip Debug Symbols Is off as well.


回答1:


If your project is using Swift, there are two separate "Optimization Level" settings in the project/target configuration.

Make sure you set them both correctly:

  1. Select your project in the Project Navigator pane
  2. Select your project's settings under the "PROJECT" tree
  3. Click "Build Settings" tab
  4. Search for "Optimization Level" and you'll see two settings, one for LLVM and one for swift.
  5. Set the appropriate setting (None [-O0] for LLVM and None [-0none] for Swift) for the build config in question.

Doing this resolved that warning for me.




回答2:


It looks like your project is in Release mode. Release mode compiles the app with lots of optimizations, but debuggers hate optimizations, so to reliably debug the app, you need to switch it to Debug mode which reduces optimization and adds a bunch of debugging information. To switch it to Debug mode:

  • Click on your scheme in the top-left corner of Xcode.

  • Select "Edit Scheme..."

  • Click on the "Build Configuration" dropdown. and change it to Debug mode.




回答3:


This warning only appear when you hit a breakpoint and the source is in a project where optimization is enabled, preventing you from watching real variable values (every object is shown as nil, even if it's not)

In my case, it only happened when debugging step by step through a cocoapod dependency.

So even if you have your main target and project settings correctly set (Strip Debug Symbol=OFF, and Optimization level None), you need to make sure it is the same for the Pod project your hitting the breakpoint from.




回答4:


Turns out that after importing an old project (Xcode 7.x+) to new Xcode 8.3 (8E162), probably due to compiler optimization, the Swift Compiler - Optimization Level was set by default to Fast, Single-File Optimization:

Changing it to none, solved the issue:




回答5:


Editor -> Validate Settings then confirm all changes. Then you should get

Set Debug to None.




回答6:


I ran into the same issue today, and figured it out (at least in my case). I'm also using CocoaPods, and I was having this issue when running my test target (Swift mixed with ObjC).

I'm using Xcode 7.2, with iOS 9.2 SDK.

In the image below, you can see the optimizations for the target and project before my change:

The surprising thing is that even though the resolved Optimization is None [-O0], only after changing the project setting from -Os to -O0 did the compiler stop optimizing the target.

Below you can see my final settings:




回答7:


This was the solution for me...

Along the lines of gimino's answer, if you are using cocoapods, add a line like this to the Podfile:

xcodeproj 'MyProject', 'Debug - local'=>:debug, 'Debug - staging'=>:debug, 'Debug - PRODUCTION'=>:debug

or for cocoapods versions >= 1.0 (thanks Diejmon)

project 'MyProject', 'Debug - local'=>:debug, 'Debug - staging'=>:debug, 'Debug - PRODUCTION'=>:debug

Where MyProject has 'Debug - local', 'Debug - staging', 'Debug - PRODUCTION' as debug configuations in addition to the standard 'Debug'

By default, cocoapods will usually generate pod configurations as Release, this Podfile line allows you to tell it they are debug.




回答8:


It's been a long time but I finally solved the issue. There is a third optimization flag LTO or Link Time Optimization and Surprisingly no one have mentioned it here and for some reason I didn't pay attention to it either. It's right there above the Optimization Level setting as you can see in many screen shots posted here.

So to summarize it there are 3 different optimization flags you want to turn off for debugging :

  • LLVM Link Time Optimization (-flto)
  • LLVM Optimization Level (-O)
  • Swift Compiler Optimization Level

More information about LTO: http://llvm.org/docs/LinkTimeOptimization.html




回答9:


Are you sure your debug configuration doesn't optimize code (it shouldn't)? It looks like you've accidentally enabled optimizations for debug configuration and you should turn it off from target's settings.




回答10:


This error happened to me twice, and in every case was a mistake in the URL parameter used to request a service. In one case the URL had some space in the port section in the other case some Optional Value wasn't being unwrapped.

So the fix was to be sure the url for the request is well formed. More information about my case, and similar reporting the same here.




回答11:


All I did is to Clean (Product > Clean) my project and run it again




回答12:


This may be an oversimplification, but are you building for Release or with optimization (which remove symbols from Swift or LLVM) too high? If so edit your scheme and switch to Debug, or edit your Build Settings for swift or LLVM optimization to None (0).




回答13:


Just in case someone is facing this issue while debugging a pod that uses a C library internally, there is another thing you have to change in the project settings to make it work in addition to everything else listed in the thread.

Go to Pods project settings -> Your C-using target -> Build Settings -> Apple Clang - Custom Compiler Flags -> Other C Flags and remove the -O3 flag that got there somehow.




回答14:


If you need to disable optimisations for your Swift pods so you can debug into them, add the following to your Podfile. This will disable the optimisations for debug builds only.

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      if config.name == "Debug"
        config.build_settings["SWIFT_OPTIMIZATION_LEVEL"] = "-Onone"
      end
    end
  end
end


来源:https://stackoverflow.com/questions/32772573/project-name-was-compiled-with-optimization-stepping-may-behave-oddly-varia

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!