xcode 未为 CocoaPods 目标定义 DEBUG 预处理器宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21046282/
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-09-15 04:32:11  来源:igfitidea点击:

DEBUG preprocessor macro not defined for CocoaPods targets

iosobjective-cxcodecocoapods

提问by Mikkel Sels?e

I'm having issues with a pod called DCIntrospect-ARC which should only work in DEBUG mode. It checks if the DEBUG macro is defined before running. However, it is not defined in the CocoaPods target and even though I am running in debug mode in Xcode it fails to run because the DEBUG macro is not defined.

我遇到了一个名为 DCIntrospect-ARC 的 pod 的问题,它只能在调试模式下工作。它在运行前检查 DEBUG 宏是否已定义。但是,它未在 CocoaPods 目标中定义,即使我在 Xcode 中以调试模式运行,它也无法运行,因为未定义 DEBUG 宏。

I can define the DEBUG macro in the podspec using

我可以使用在 podspec 中定义 DEBUG 宏

s.xcconfig = { "GCC_PREPROCESSOR_DEFINITIONS" => '$(inherited) DEBUG=1' }

but this defined DEBUG for all build configurations and not only the DEBUG configuration.

但这定义了所有构建配置的调试,而不仅仅是调试配置。

  1. Is this a CocoaPods issue? Shouldn't the DEBUG macro generally be defined for Pods?
  2. Can I work around this in the Podspec file and declare the DEBUG macro in the Debug build configuration only?
  1. 这是 CocoaPods 的问题吗?DEBUG 宏一般不应该为 Pod 定义吗?
  2. 我可以在 Podspec 文件中解决这个问题并仅在 Debug 构建配置中声明 DEBUG 宏吗?

回答by johndpope

you can use the post_install hook in Podfile.

你可以在 Podfile 中使用 post_install 钩子。

This hook allows you to make any last changes to the generated Xcode project before it is written to disk, or any other tasks you might want to perform.http://guides.cocoapods.org/syntax/podfile.html#post_install

此挂钩允许您在将生成的 Xcode 项目写入磁盘之前对生成的 Xcode 项目进行任何最后更改,或者您可能想要执行的任何其他任务。http://guides.cocoapods.org/syntax/podfile.html#post_install

post_install do |installer_representation|
    installer_representation.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            if config.name != 'Release'
                config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'DEBUG=1']
            end
        end
    end
end

回答by Antoine

Thanks to John I completed my custom Podfile script, which also changes the optimization level to zero and enables assertions.

感谢 John 我完成了我的自定义 Podfile 脚本,它还将优化级别更改为零并启用断言。

I've got multiple debug configurations (for ACC and PROD), so I needed to update several properties for debugging purposes.

我有多个调试配置(用于 ACC 和 PROD),因此我需要更新几个属性以进行调试。

post_install do |installer|
  installer.pods_project.build_configurations.each do |config|
    if config.name.include?("Debug")
      # Set optimization level for project
      config.build_settings['GCC_OPTIMIZATION_LEVEL'] = '0'

      # Add DEBUG to custom configurations containing 'Debug'
      config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
      if !config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'].include? 'DEBUG=1'
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'DEBUG=1'
      end
    end
  end

  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      if config.name.include?("Debug")
        # Set optimization level for target
        config.build_settings['GCC_OPTIMIZATION_LEVEL'] = '0'
        # Add DEBUG to custom configurations containing 'Debug'
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
        if !config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'].include? 'DEBUG=1'
          config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'DEBUG=1'
        end
        # Enable assertions for target
        config.build_settings['ENABLE_NS_ASSERTIONS'] = 'YES'

        config.build_settings['OTHER_CFLAGS'] ||= ['$(inherited)']
        if config.build_settings['OTHER_CFLAGS'].include? '-DNS_BLOCK_ASSERTIONS=1'
          config.build_settings['OTHER_CFLAGS'].delete('-DNS_BLOCK_ASSERTIONS=1')
        end
      end
    end
  end
end

回答by xaphod

The accepted answer as of now doesn't work for Swift Pods. Here is a one-line change to that answer that appears to work for both.

目前接受的答案不适用于 Swift Pods。这是对该答案的一行更改,似乎对两者都适用。

    post_install do |installer_representation|
        installer_representation.pods_project.targets.each do |target|
            target.build_configurations.each do |config|
                if config.name != 'Release'
                    config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'DEBUG=1']
                    config.build_settings['OTHER_SWIFT_FLAGS'] = ['$(inherited)', '-DDEBUG']
                end
            end
        end
    end

回答by Victor Choy

I think the accepted answer is not so right for me. config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'DEBUG=1']

我认为接受的答案对我来说不太合适。 config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'DEBUG=1']

||=is used to assign a empty or nil variable, but if the config.build_settings['GCC_PREPROCESSOR_DEFINITIONS']is not empty ?

||=用于分配一个空或 nil 变量,但如果 config.build_settings['GCC_PREPROCESSOR_DEFINITIONS']不为空?

The array cannot be modified at all. The value is ["POD_CONFIGURATION_PRODUCTION=1", "$(inherited)"]for me.

根本无法修改数组。价值是["POD_CONFIGURATION_PRODUCTION=1", "$(inherited)"]给我的。

So I gave the complete anwser.

所以我给出了完整的答案。

post_install do |installer_representation|
    installer_representation.pods_project.build_configurations.each do |config|
        if config.name == 'Release' || config.name == 'Production' || config.name == 'Release-InHouse'
          config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= []
          config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] |= ['$(inherited)', 'NDEBUG=1']
        end
    end
end 

||= [] make sure the variable is a valid array. and arrayA |= arrayBmeans arrayA + arrayB and rid the the repeated element, and then return to arrayA.

||= [] 确保变量是一个有效的数组。andarrayA |= arrayB表示 arrayA + arrayB 并去掉重复的元素,然后返回到 arrayA。

回答by delrox

Even easier: just ensure you have the DEBUG=1macro to your GCC_PREPROCESSOR_DEFINITIONS in your project in xCode for debug mode but not release mode. If you add it the the project level (not specific targets) it will be inherited by all targets (debug-test, custom targets, etc). This is set by default on new projects, and generally expected to be there. If you are missing it, that could have broad impact.

更简单:只需确保DEBUG=1在 xCode 中的项目中具有GCC_PREPROCESSOR_DEFINITIONS的宏,用于调试模式而不是发布模式。如果将它添加到项目级别(不是特定目标),它将被所有目标(调试测试、自定义目标等)继承。这是新项目的默认设置,并且通常会出现在那里。如果你错过了它,那可能会产生广泛的影响。

If it's still not working, ensure you also have $(inherited)in all your targets for GCC_PREPROCESSOR_DEFINITIONS. CocoaPods and DEBUG both count on that.

如果它仍然不起作用,请确保您的$(inherited)所有目标中也有GCC_PREPROCESSOR_DEFINITIONS。CocoaPods 和 DEBUG 都依赖于此。

settings

设置