在命名空间'std'中找不到名为'unary_function'的模板;你是不是想说'__unary_function'?

56
刚刚升级了我的Xcode到15.0,突然间它在RCT_Folly中给了我以下错误: 在命名空间'std'中找不到名为'unary_function'的模板;你是不是想说'__unary_function'? 这是出错的那一行代码:

struct hash_base : std::unary_function<T, std::size_t> {};

我尝试过清除缓存数据和派生数据,以及清理构建。也尝试过移除pods和node_modules。但是没有任何帮助。
10个回答

63

enter image description here

在我的情况下,构建失败后我遵循了Xcode的建议。我只是点击了“修复”,现在一切都正常工作了。
希望在下次更新中能够修复这个问题。

不知怎么的,当我按下修复按钮时,我的Xcode就会卡住。之后构建或运行菜单都无法正常工作。我不得不强制关闭Xcode,一旦再次启动,它就会显示相同的错误。 - undefined

30
在源文件中修复之前,最好的选择是在您的podfile中添加被删除的libcpp函数,解决方法在这里https://github.com/facebook/react-native/issues/37748#issuecomment-1580589448
  post_install do |installer|
      installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
          config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', '_LIBCPP_ENABLE_CXX17_REMOVED_UNARY_BINARY_FUNCTION']
        end
      end
  end

27
在运行npx react-native run-ios后,我在终端中遇到了这个错误。我通过以下步骤解决了它:
  1. 打开Xcode
  2. 点击播放图标启动模拟器
  3. 模拟器会失败,但Xcode会在侧边栏显示错误。点击错误以查看代码中的错误位置。
  4. 点击“修复”按钮
  5. 完成!现在模拟器将成功运行。

enter image description here


当我按下修复按钮时,我的Xcode会卡住。之后构建或运行菜单也无法正常工作。我不得不强制关闭Xcode,然后重新启动它,但它仍然显示相同的错误。 - undefined
@MiteshBaraiya 看起来你在Xcode中手动编辑了代码行(根据你在下面发布的答案)。这可能是为什么在你的情况下点击“修复”无效的原因? - undefined

16
这有点傻,但我按照XCode的建议将__unary_function放在了那里,问题就解决了。
所以实际上的代码行是这样的:

struct hash_base : std::__unary_function<T, std::size_t> {};


2
是的,我现在也遇到了这个问题 - 这个“修复”有效,我想知道它是否会被纳入 RN 的官方版本中... - undefined
2
同意 @MattRoberts,每次 Xcode 更新都会出现新的愚蠢错误。 - undefined
1
@MiteshBaraiya 这不是一个“愚蠢的错误”。这在C++11中已经被弃用,并在C++17中被移除。我猜Clang刚刚赶上了C++17。 - undefined
@JeremyP 真是令人惊讶,你总是那么迅速地下结论。 我给你举个例子。这是一个图书馆的链接:https://github.com/cvg/limap/ - 这是一个全新的计算机视觉算法,CVPR2023会议上进行了介绍。这是最新的研究代码,刚刚编写完毕。在 Xcode 15 更新之前,它在 Linux 和 MacOS 上运行得非常完美。 它使用了最新版本的 Boost,通过 brew install 安装的。 所以,我不应该使用 Boost 吗?或者 Boost(作为最受欢迎的 C++ 开发工具之一)的开发者们需要考虑他们的优先事项吗? - undefined
@old-ufo 不要责怪Clang实现C++17(六年前),要责怪那些忽视了十二年废弃警告的库的维护者。没有任何借口。 - undefined
显示剩余6条评论

8
所有的库都必须更新他们的代码,使用std::function和std::bind来替代unary_function。
解决方法:
选择Pods > Build Settings > 在Apple Clang - Preprocessing部分,在宏的部分下面。

preview add line

在发布和调试中添加 -> _LIBCPP_ENABLE_CXX17_REMOVED_UNARY_BINARY_FUNCTION
附言:要添加这个,只需点击列,然后在底部点击"+" -> 然后粘贴:_LIBCPP_ENABLE_CXX17_REMOVED_UNARY_BINARY_FUNCTION preview add line

我之前尝试过使用GitHub上的参考资料,但不幸的是,对我来说没有起作用。希望它能对其他人有用。 - undefined
还有问题吗?想和我分享吗?@MiteshBaraiya - undefined
我通过将'unary_function'替换为'__unary_function'来解决了这个问题。@tetar - undefined

4
@Ash 是对的,我更新了我的Xcode和iOS版本到17.0。一切都正常运行,但是当我在iOS 17上运行时,出现了这个错误。
No template named 'unary_function' in namespace 'std'; did you mean '__unary_function'

我按照提供的步骤来修复它,应用程序开始运行了。 然而,当我打开使用react-native-maps的地图屏幕时,它崩溃了。 之后,我删除了pod,重新安装了节点模块,但是问题仍然存在。 令人惊讶的是,当我将以下代码添加到我的Podfile并重新安装pod后,应用程序开始正常运行。 你必须修复两个问题。
 post_install do |installer|
      installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
          config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', '_LIBCPP_ENABLE_CXX17_REMOVED_UNARY_BINARY_FUNCTION']
        end
      end
  end

1
如果你可以控制编译标志,你有两个选择:
1. 让clang重新启用已弃用的函数:-D_LIBCPP_ENABLE_CXX17_REMOVED_UNARY_BINARY_FUNCTION
2. 阻止Boost使用已弃用的函数:-DBOOST_NO_CXX98_FUNCTION_BASE

1

请在您的回答中添加细节。 - undefined

1
对于遇到这个错误的React Native用户,Facebook在v0.72.5版本中发布了修复方法-发布说明

XCode 15修复(21763e85e3,0dbd621c59和8a5b2d6735)

npm update react-native


即使更新了我的项目,仍然出现相同的错误... :-( - undefined

1
如果你还在使用旧版本的RN,
这对我有用:
post_install do |installer|
        react_native_post_install(installer)
        __apply_Xcode_12_5_M1_post_install_workaround(installer)
        
        installer.pods_project.build_configurations.each do |config|
            installer.pods_project.targets.each do |target|
                if target.respond_to?(:product_type) and target.product_type == "com.apple.product-type.bundle"
                    config.build_settings['CODE_SIGNING_ALLOWED'] = 'NO'
                end
            end
    
            # Set the preprocessing macro for the whole Pods project
            existing_flags = config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
            existing_flags << '_LIBCPP_ENABLE_CXX17_REMOVED_UNARY_BINARY_FUNCTION'
            config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] = existing_flags
        end
    end

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