首页 文章

React Native iOS:无法构建模块'yoga':'algorithm'文件未找到

提问于
浏览
7

我正在尝试使用此集成指南将React Native集成到现有的Swift iOS应用程序中:https://facebook.github.io/react-native/docs/integration-with-existing-apps.html . React Native的版本是0.53.0 .

我已经成功安装了所有必需的pod,现在尝试构建项目,但总是得到以下编译错误:

错误日志:

While building module 'yoga' imported from ...node_modules/react-native/React/Base/RCTConvert.h:19:
In file included from <module-includes>:1:
In file included from ...ios/Vandebron/Pods/Target Support Files/yoga/yoga-umbrella.h:15:
In file included from ...node_modules/react-native/ReactCommon/yoga/yoga/YGNode.h:13:
...node_modules/react-native/ReactCommon/yoga/yoga/Yoga-internal.h:11:10: fatal error: 'algorithm' file not found
#include <algorithm>
         ^~~~~~~~~~~
1 error generated.
In file included from ...node_modules/react-native/React/Views/RCTActivityIndicatorViewManager.m:10:
In file included from ...node_modules/react-native/React/Views/RCTActivityIndicatorViewManager.h:10:
In file included from ...node_modules/react-native/React/Views/RCTViewManager.h:13:
...node_modules/react-native/React/Base/RCTConvert.h:19:9: fatal error: could not build module 'yoga'
#import <yoga/Yoga.h

2 回答

  • 10

    您可以将以下代码段放入Podfile中以修复此问题以及React Native Cocoapods的其他一些已知问题 . 我们在自己的项目中对它进行了测试,结果非常好:

    # When using RN in combination with Cocoapods, a lot of
    # things are broken. These are the fixes we had to append
    # to our Podfile when upgrading to ReactNative@0.55.4.
    #
    # WARNING: Check those line numbers when you're on a different version!
    
    def change_lines_in_file(file_path, &change)
        print "Fixing #{file_path}...\n"
    
        contents = []
    
        file = File.open(file_path, 'r')
        file.each_line do | line |
            contents << line
        end
        file.close
    
        File.open(file_path, 'w') do |f|
            f.puts(change.call(contents))
        end
    end
    
    
    post_install do |installer|
    
        # https://github.com/facebook/yoga/issues/711#issuecomment-381098373
        change_lines_in_file('./Pods/Target Support Files/yoga/yoga-umbrella.h') do |lines|
            lines.reject do | line |
                [
                '#import "Utils.h"',
                '#import "YGLayout.h"',
                '#import "YGNode.h"',
                '#import "YGNodePrint.h"',
                '#import "YGStyle.h"',
                '#import "Yoga-internal.h"',
                ].include?(line.strip)
            end
        end
    
        # https://github.com/facebook/yoga/issues/711#issuecomment-374605785
        change_lines_in_file('../../node_modules/react-native/React/Base/Surface/SurfaceHostingView/RCTSurfaceSizeMeasureMode.h') do |lines|
            unless lines[27].include?("#ifdef __cplusplus")
                lines.insert(27, "#ifdef __cplusplus")
                lines.insert(34, "#endif")
            end
            lines
        end
    
        # https://github.com/facebook/react-native/issues/13198
        change_lines_in_file('../../node_modules/react-native/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.h') do |lines|
            lines.map { |line| line.include?("#import <RCTAnimation/RCTValueAnimatedNode.h>") ? '#import "RCTValueAnimatedNode.h"' : line }
        end
    
        # https://github.com/facebook/react-native/issues/16039
        change_lines_in_file('../../node_modules/react-native/Libraries/WebSocket/RCTReconnectingWebSocket.m') do |lines|
            lines.map { |line| line.include?("#import <fishhook/fishhook.h>") ? '#import "fishhook.h"' : line }
        end
    end
    

    源代码取自这个要点:https://gist.github.com/Jpunt/3fe75effd54a702034b75ff697e47578

  • 1

    这是一个cocoapods问题 . 尝试通过编辑yoga.podspec文件来公开所需的 Headers :node_modules / react-native / ReactCommon / yoga / yoga.podspec在yoga.podspec结束时添加此行:

    spec.public_header_files ='yoga / Yoga.h','yoga / YGEnums.h',

    看起来像这样:

    source_files = 'yoga/**/*.{cpp,h}'
           source_files = File.join('ReactCommon/yoga', source_files) if ENV['INSTALL_YOGA_WITHOUT_PATH_OPTION']       
           spec.source_files = source_files    spec.source_files = source_files
    +
    +  # Only expose the needed headers
    +  spec.public_header_files = 'yoga/Yoga.h', 'yoga/YGEnums.h', 'yoga/YGMacros.h'
    +
    end
    

    参考:https://github.com/facebook/react-native/issues/17893

    实际拉取请求:https://github.com/facebook/react-native/pull/17764 . 请查看 Plo4ox 的评论 .

相关问题