macos - 错误 : 'uncaught_exceptions' is unavailable: introduced in macOS 10. 12

标签 macos exception clang c++17 feature-detection

我正在测试最近在 C++17 下测试期间出现的问题。这是源文件:

$ cat test.cxx
#if __EXCEPTIONS && __has_feature(cxx_exceptions)
# include <exception>
# define CXX17_EXCEPTIONS 1
#endif

void Foo()
{
#if defined(CXX17_EXCEPTIONS)
    if (std::uncaught_exceptions() == 0)
#endif
    {
        int x = 0;
    }
}

并在 OS X 10.8 或 10.9 上使用 Macports 编译器编译它:

$ /opt/local/bin/clang++-mp-5.0 -std=gnu++17 test.cxx -c
test.cxx:9:14: error: 'uncaught_exceptions' is unavailable: introduced in macOS 10.12
    if (std::uncaught_exceptions() == 0)
             ^
/opt/local/libexec/llvm-5.0/include/c++/v1/exception:130:63: note:
      'uncaught_exceptions' has been explicitly marked unavailable here
_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_UNCAUGHT_EXCEPTIONS int uncaught_e...
                                                              ^
1 error generated.

/opt/local/bin/clang++-mp-5.0 也遇到了这个问题。这似乎不是一次性的问题。 4.0 拒绝 -std=gnu++17 所以我无法进一步测试。

根据 Clang 3.6 Release Notes ,我相信我对 std::uncaught_exceptions() 使用了正确的测试:

To reliably test if C++ exceptions are enabled, use __EXCEPTIONS && __has_feature(cxx_exceptions), else things won’t work in all versions of Clang in Objective-C++ files.

我可能无法包含 Apple 特定的头文件,因此我无法针对 OS X 10.12 进行测试。其他人遇到过这个问题,但我还没有找到合适的解决方案。比如这个bug report有同样 Unresolved 问题。

有没有办法在包含标准 C++ 预处理器宏和功能测试的 Apple 平台上解决此问题?如果是这样,方法或测试是什么?


$ /opt/local/bin/clang++-mp-6.0 --version
clang version 6.0.1 (tags/RELEASE_601/final)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
InstalledDir: /opt/local/libexec/llvm-6.0/bin

$ /opt/local/bin/clang++-mp-5.0 --version
clang version 5.0.2 (tags/RELEASE_502/final)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
InstalledDir: /opt/local/libexec/llvm-5.0/bin

最佳答案

这里的问题是 Mac OS 10.12 之前的运行时库 (libc++.dylib) 不包含 std::uncaught_exceptions() 的定义。如果您尝试在这些平台上使用它,则会导致链接器错误。因此,我们会在编译时警告您这会失败。这与异常可用/不可用无关:它与特定功能不可用有关。

关于macos - 错误 : 'uncaught_exceptions' is unavailable: introduced in macOS 10. 12,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53253553/

相关文章:

java - ExceptionReport.tml 中的 Tapestry 5.4 异常

Django + Postgresql -> 未处理的异常

c++ - 使用 clang 编译时出现正则表达式段错误,可能是编译器错误?

vim - 如何使用 coc-clangd 在 vi​​m 中使用 lib gtk 进行 C 编程?

Clang sqlite3浮点错误

objective-c - 带有子类的 NSSecureTextField 中的文本垂直居中

macos - 能够通过 Cyber​​duck SSH 到 EC2,但无法使用相同的凭据通过终端 SSH

macos - 显示新文档窗口的工作表

macos - applescript 通过命令行将歌曲添加到 iTunes 播放列表

c# - 你能在 C# 代码中捕获 native 异常吗?