Xcode 9抛出涉及'require'的错误

Dan*_*ker 9 xcode xcode9

升级代码以在Xcode 9下构建时,我看到代码中的编译错误使用requirerequire_noerr:

    require(length > offsetof(struct blob, cert), outLabel);
Run Code Online (Sandbox Code Playgroud)

第一个错误是: error: implicit declaration of function 'require' is invalid in C99

我也得到了很多error: use of undeclared identifier 'outLabel'.这是在RRTransactionVerifier.m中,这是用于处理收据验证的Apple代码.

我该如何解决这些错误?

Dan*_*ker 24

require以及require_noerr曾经在AssertMacros.h中定义的宏.从Xcode 9开始,这些宏发生了变化.

原因记录在该头文件中:

对于远古时代,Mac OS X已经定义了大多数这些宏的版本,没有__前缀,这可能会与用户代码中类似命名的函数或宏发生冲突,包括Boost和C++标准库中的新功能.

macOS High Sierra和iOS 11现在要求客户端移动到上面定义的新宏.

如果要在自己的项目中启用宏,可以通过Xcode构建配置定义__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES宏.请参阅Xcode帮助中的"添加构建配置(xcconfig)文件".

因此,要解决此问题,您可以设置定义或更改代码以使用新宏. require现在__Require,require_noerr现在__Require_noErr,等等.它们在头文件中提供了有关如何通过脚本更改代码的脚本:

编辑:在脚本的第一行末尾添加一个反斜杠,否则终端会将命令分成两个.

/*
To aid users of these macros in converting their sources, the following tops script 
will convert usages of the old macros into the new equivalents.  To do so, in Terminal 
go into the directory containing the sources to be converted and run this command.

find . -name '*.[c|cc|cp|cpp|m|mm|h]' -print0 |  xargs -0 tops \
-verbose \
      replace "check(<b args>)" with "__Check(<args>)" \
      replace "check_noerr(<b args>)" with "__Check_noErr(<args>)" \
      replace "check_noerr_string(<b args>)" with "__Check_noErr_String(<args>)" \
      replace "check_string(<b args>)" with "__Check_String(<args>)" \
      replace "require(<b args>)" with "__Require(<args>)" \
      replace "require_action(<b args>)" with "__Require_Action(<args>)" \
      replace "require_action_string(<b args>)" with "__Require_Action_String(<args>)" \
      replace "require_noerr(<b args>)" with "__Require_noErr(<args>)" \
      replace "require_noerr_action(<b args>)" with "__Require_noErr_Action(<args>)" \
      replace "require_noerr_action_string(<b args>)" with "__Require_noErr_Action_String(<args>)" \
      replace "require_noerr_string(<b args>)" with "__Require_noErr_String(<args>)" \
      replace "require_string(<b args>)" with "__Require_String(<args>)" \
      replace "verify(<b args>)" with "__Verify(<args>)" \
      replace "verify_action(<b args>)" with "__Verify_Action(<args>)" \
      replace "verify_noerr(<b args>)" with "__Verify_noErr(<args>)" \
      replace "verify_noerr_action(<b args>)" with "__Verify_noErr_Action(<args>)" \
      replace "verify_noerr_string(<b args>)" with "__Verify_noErr_String(<args>)" \
      replace "verify_string(<b args>)" with "__Verify_String(<args>)" \
      replace "ncheck(<b args>)" with "__nCheck(<args>)" \
      replace "ncheck_string(<b args>)" with "__nCheck_String(<args>)" \
      replace "nrequire(<b args>)" with "__nRequire(<args>)" \
      replace "nrequire_action(<b args>)" with "__nRequire_Action(<args>)" \
      replace "nrequire_action_quiet(<b args>)" with "__nRequire_Action_Quiet(<args>)" \
      replace "nrequire_action_string(<b args>)" with "__nRequire_Action_String(<args>)" \
      replace "nrequire_quiet(<b args>)" with "__nRequire_Quiet(<args>)" \
      replace "nrequire_string(<b args>)" with "__nRequire_String(<args>)" \
      replace "nverify(<b args>)" with "__nVerify(<args>)" \
      replace "nverify_string(<b args>)" with "__nVerify_String(<args>)" \
      replace "require_action_quiet(<b args>)" with "__Require_Action_Quiet(<args>)" \
      replace "require_noerr_action_quiet(<b args>)" with "__Require_noErr_Action_Quiet(<args>)" \
      replace "require_noerr_quiet(<b args>)" with "__Require_noErr_Quiet(<args>)" \
      replace "require_quiet(<b args>)" with "__Require_Quiet(<args>)" \
      replace "check_compile_time(<b args>)" with "__Check_Compile_Time(<args>)" \
      replace "debug_string(<b args>)" with "__Debug_String(<args>)"
*/
Run Code Online (Sandbox Code Playgroud)