ios “项目名称”是经过优化编译的 - 步进可能会表现得很奇怪;变量可能不可用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32772573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:49:28  来源:igfitidea点击:

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

iosafnetworking-2xcode7

提问by Mojtaba

Trying to step into AFNetworking code generates following warning:

尝试进入 AFNetworking 代码会生成以下警告:

[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+AFNetworkingcategory 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+TVASTAFNetworkingas category name which does not exist anywhere in the code base.

当然,我无法调试代码。具体来说,我正在尝试调试UIImageView+AFNetworking似乎不可能的类别。更改代码没有效果(尝试过NSLog等),并且当尝试进入编译器时,编译器会转到汇编代码并显示UIImageView+TVASTAFNetworking为代码库中任何地方都不存在的类别名称。

enter image description here

在此处输入图片说明

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

使用 Xcode 7. iOS 9 & 8. Cocoapods (无框架)

UPDATEI forgot to mention that Optimizer is set to nonefor both release and debug configuration and I am in fact using Debugconfig.

更新我忘了提到优化器设置none为发布和调试配置,实际上我正在使用Debug配置。

enter image description here

在此处输入图片说明

UPDATE 2

更新 2

Strip Debug SymbolsIs off as well.

Strip Debug Symbols也关了。

采纳答案by Mojtaba

It's been a long time but I finally solved the issue. There is a third optimization flag LTOor Link Time Optimizationand 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 Levelsetting as you can see in many screen shots posted here.

已经很长时间了,但我终于解决了这个问题。还有第三个优化标志,LTO或者Link Time Optimization令人惊讶的是这里没有人提到它,出于某种原因我也没有注意它。Optimization Level正如您在此处发布的许多屏幕截图中看到的那样,它就在设置上方。

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

总结一下,您需要关闭 3 个不同的优化标志以进行调试:

  • LLVM Link Time Optimization (-flto)
  • LLVM Optimization Level (-O)
  • Swift Compiler Optimization Level
  • LLVM 链接时间优化 ( -flto)
  • LLVM 优化级别 ( -O)
  • Swift 编译器优化级别

enter image description here

在此处输入图片说明

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

有关 LTO 的更多信息:http: //llvm.org/docs/LinkTimeOptimization.html

回答by Albert Bori

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

如果您的项目使用 Swift,则项目/目标配置中有两个单独的“优化级别”设置。

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.
  1. 在 Project Navigator 窗格中选择您的项目
  2. 在“PROJECT”树下选择您的项目设置
  3. 单击“构建设置”选项卡
  4. 搜索“优化级别”,您将看到两种设置,一种用于 LLVM,一种用于 swift。
  5. 为有问题的构建配置设置适当的设置(None [-O0]对于 LLVM 和None [-0none]Swift)。

was compiled with optimization stepping may behave oddlyvariables may not be available

使用优化步进编译可能表现奇怪变量可能不可用

Doing this resolved that warning for me.

这样做为我解决了这个警告。

回答by NobodyNada - Reinstate Monica

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.
  • 在 Xcode 的左上角单击您的方案。

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

在 Xcode 的左上角单击您的方案。

  • Select "Edit Scheme..."
  • 选择“编辑方案...”

Select "Edit Scheme..."

选择“编辑方案...”

  • Click on the "Build Configuration" dropdown. and change it to Debug mode.
  • 单击“构建配置”下拉列表。并将其更改为调试模式。

Click on the "Build Configuration" dropdown.

单击“构建配置”下拉列表。

回答by Gomino

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)

此警告仅在您遇到断点且源位于启用优化的项目中时出现,从而阻止您查看实际变量值(每个对象都显示为 nil,即使不是)

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

就我而言,它仅在通过 cocoapod 依赖项逐步调试时发生。

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.

因此,即使您正确设置了主要目标和项目设置(Strip Debug Symbol=OFF 和优化级别 None),您也需要确保它与您从中点击断点的 Pod 项目相同。

enter image description here

在此处输入图片说明

回答by valvoline

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 Levelwas set by default to Fast, Single-File Optimization:

原来,在将旧项目 (Xcode 7.x+) 导入新的 Xcode 8.3 (8E162) 后,可能是由于编译器优化,Swift Compiler - Optimization Level默认设置为Fast, Single-File Optimization

Before

前

Changing it to none, solved the issue:

将其更改为无,解决了问题:

after

后

回答by james sa

Editor-> Validate Settingsthen confirm all changes. Then you should get Swift Compiler Optimisation Level in place

Editor->Validate Settings然后确认所有更改。那么你应该得到Swift 编译器优化级别到位

Set Debug to None.

将调试设置为None.

回答by André Neves

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

我今天遇到了同样的问题,并解决了(至少在我的情况下)。我也在使用 CocoaPods,并且在运行我的测试目标(Swift 与 ObjC 混合)时遇到了这个问题。

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

我正在使用 Xcode 7.2 和 iOS 9.2 SDK。

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

在下图中,您可以看到我更改之前目标和项目的优化:

optimization level before change

更改前的优化级别

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

令人惊讶的是,即使解析出的 Optimization 是None [-O0],只有在将项目设置从-Os更改为-O0之后,编译器才停止优化目标。

Below you can see my final settings:

您可以在下面看到我的最终设置:

optimization level after change

更改后的优化级别

回答by wils

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:

沿着 gimino 的回答,如果您使用的是 cocoapods,请在 Podfile 中添加如下一行:

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

or for cocoapods versions >= 1.0 (thanks Diejmon)

或者对于 cocoapods 版本 >= 1.0(感谢 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'

除了标准的“调试”之外,MyProject 还具有“调试 - 本地”、“调试 - 登台”、“调试 - 生产”作为调试配置

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

默认情况下,cocoapods 通常会生成 pod 配置作为 Release,此 Podfile 行允许您告诉它它们正在调试。

回答by Al Priest

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.

如果您需要禁用 Swift pod 的优化以便您可以调试它们,请将以下内容添加到您的Podfile. 这将仅禁用调试版本的优化。

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

回答by Can Poyrazo?lu

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.

你确定你的调试配置没有优化代码(它不应该)?看起来您不小心启用了调试配置的优化,您应该从目标的设置中将其关闭。