DEBUG preprocessor macro not defined for CocoaPods targets

后端 未结 5 780
灰色年华
灰色年华 2020-12-29 07:01

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 define

相关标签:
5条回答
  • 2020-12-29 07:28

    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.

        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
    
    0 讨论(0)
  • 2020-12-29 07:33

    you can use the post_install hook in Podfile.

    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

    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
    
    0 讨论(0)
  • 2020-12-29 07:36

    Even easier: just ensure you have the DEBUG=1 macro 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.

    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.

    0 讨论(0)
  • 2020-12-29 07:39

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

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

    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
    
    0 讨论(0)
  • 2020-12-29 07:40

    I think the accepted answer is not so right for me. 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 ?

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

    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 |= arrayB means arrayA + arrayB and rid the the repeated element, and then return to arrayA.

    0 讨论(0)
提交回复
热议问题