CocoaPods目标中未定义DEBUG预处理器宏。

23

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

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

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

但是这会为所有的构建配置定义DEBUG,而不仅仅是DEBUG配置。

  1. 这是 CocoaPods 的问题吗? DEBUG 宏通常不应该为 Pods 定义吗?
  2. 我能否在 Podspec 文件中解决这个问题,并仅在调试构建配置中声明 DEBUG 宏?
5个回答

25

您可以在Podfile中使用post_install hook。

此钩子允许您在生成的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

3
在使用Cocoapods版本1.0.0.beta.3时,我需要使用installer_representation.pods_project而不是installer_representation.project。 - Amos Joshua
1
这对于Swift Pods不起作用。我在下面添加了一个答案,增加了一行代码,因此它也适用于Swift Pods。 - xaphod

13

感谢 John 的帮助,我完成了自定义的 Podfile 脚本,同时将优化级别设置为零并启用了断言。

我有多个调试配置(针对 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

2
我认为接受的答案对我来说不太正确。 config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'DEBUG=1'] ||= 用于分配一个空或nil变量,但如果 config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] 不为空呢?
数组根本无法被修改。对我来说,该值为["POD_CONFIGURATION_PRODUCTION=1", "$(inherited)"]
因此,我给出了完整的答案。
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 

||=[] 确保变量是一个有效的数组。而arrayA |= arrayB表示将arrayB中的元素添加到arrayA中,并去除重复元素,最后返回arrayA。


2
截至目前,已被接受的答案对于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

OTHER_SWIFT_FLAGS非常重要,它对我的Swift Pod起作用了。 - Dmitry Kuznetsov

-2
更简单的方法是:在xCode项目中为调试模式添加DEBUG=1宏到你的GCC_PREPROCESSOR_DEFINITIONS中即可。如果你将其添加到项目级别(而不是特定目标),它将被所有目标继承(调试测试、自定义目标等)。这在新项目中默认设置,并且通常期望存在。如果你缺少它,可能会产生广泛的影响。
如果仍然无法正常工作,请确保在所有目标的GCC_PREPROCESSOR_DEFINITIONS中也有$(inherited)。CocoaPods和DEBUG都依赖于此。

settings


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接