没有为CocoaPods目标定义DEBUG预处理器宏

时间:2014-01-10 14:02:10

标签: ios objective-c xcode cocoapods

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

我可以使用

在podspec中定义DEBUG宏
s.xcconfig = { "GCC_PREPROCESSOR_DEFINITIONS" => '$(inherited) DEBUG=1' }

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

  1. 这是一个CocoaPods问题吗?通常不应该为Pods定义DEBUG宏吗?
  2. 我可以在Podspec文件中解决此问题,并仅在Debug构建配置中声明DEBUG宏吗?

5 个答案:

答案 0 :(得分:21)

您可以在Podfile中使用post_install挂钩。

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

答案 1 :(得分:11)

感谢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 :(得分:1)

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

答案 3 :(得分:1)

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

||=用于指定空或nil变量,但如果config.build_settings['GCC_PREPROCESSOR_DEFINITIONS']不为空?

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

所以我给了完整的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 

|| = []确保变量是有效数组。并且arrayA |= arrayB表示arrayA + arrayB并删除重复的元素,然后返回到arrayA。

答案 4 :(得分:-2)

更简单:只需确保您的项目中的GCC_PREPROCESSOR_DEFINITIONS的DEBUG=1宏在xCode中用于调试模式,但不是释放模式。如果将项目级别(不是特定目标)添加到它,它将由所有目标(调试测试,自定义目标等)继承。默认情况下,这是在新项目中设置的,通常预计会在那里。如果你错过它,那可能会产生广泛的影响。

如果它仍无法正常工作,请确保GCC_PREPROCESSOR_DEFINITIONS的所有目标中都有$(inherited)。 CocoaPods和DEBUG都指望它。

settings