我有一台运行El Capitan 10.11.6版的旧Mac(大约在2009年)。 Apple不允许在我的计算机上对其操作系统进行进一步的更新,但是通过Macports,它可以为非Apple专用软件提供良好的开发环境。

我使用现成的支持std::filesystem的g++ 9.2以及不支持的clang 8.0进行编译。 (在每种情况下,都使用每个编译器的 native 标准库。)而且我正在使用--std = c++ 2a进行编译。

我注意到llvm 9应该开箱即用支持std::filesystem(https://libcxx.llvm.org/docs/UsingLibcxx.html#using-filesystem),所以我通过Macports下载了clang/llvm 9。不幸的是,我遇到了障碍。

重现错误的最少代码是cppreference.com(https://en.cppreference.com/w/cpp/filesystem/path/path)中示例的简化形式

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int
main()
{
   fs::path p1 = "/usr/lib/sendmail.cf";

   std::cout << "p1 = " << p1 << '\n';
}

这是CmakeLists.txt
cmake_minimum_required(VERSION 3.15.5)
project(bug LANGUAGES CXX)
add_executable (bug main.cpp)
target_compile_options(bug PRIVATE "-std=c++2a")

这是编译器的提示:
[build] Starting build
[proc] Executing command: /opt/local/bin/cmake --build ~/temp/build/debug/clang --config debug --target all -- -j 10
[build] [1/2  50% :: 1.598] Building CXX object CMakeFiles/bug.dir/main.cpp.o
[build] FAILED: CMakeFiles/bug.dir/main.cpp.o
[build] /opt/local/bin/clang++    -g   -std=c++2a -MD -MT CMakeFiles/bug.dir/main.cpp.o -MF CMakeFiles/bug.dir/main.cpp.o.d -o CMakeFiles/bug.dir/main.cpp.o -c ../../../main.cpp
[build] ../../../main.cpp:9:8: error: 'path' is unavailable: introduced in macOS 10.15
[build]    fs::path p1 = "/usr/lib/sendmail.cf";
[build]        ^
[build] /opt/local/libexec/llvm-9.0/bin/../include/c++/v1/filesystem:738:24: note: 'path' has been explicitly marked unavailable here
[build] class _LIBCPP_TYPE_VIS path {
...

向后工作,我在/opt/local/libexec/llvm-9.0/include/c++/v1/__config中找到以下代码:
#  define _LIBCPP_AVAILABILITY_FILESYSTEM                                      \
     __attribute__((availability(macosx,strict,introduced=10.15)))             \
     __attribute__((availability(ios,strict,introduced=13.0)))                 \
     __attribute__((availability(tvos,strict,introduced=13.0)))                \
     __attribute__((availability(watchos,strict,introduced=6.0)))

据我确定,此#define是上述错误消息的最终原因。

因此,我的问题是:
  • 这是LLVM的错误吗?毕竟,GCC不会在std::filesystem和OS版本之间引入依赖关系。
  • 这是Macports的错误吗?也许他们在构建时没有使用正确的标志?
  • 如果我 native 构建LLVM和Clang,我可以解决此问题吗?
  • 根本不是问题吗?也许LLVM的好人知道一些GCC的好人不知道的东西。

  • 注意:还有一个类似的问题,涉及通过Homebrew下载的clang/llvm。不幸的是,评论没有帮助。 [LLVM-9 clang-9 OSX]: std::filesystem::path unrecognized

    最佳答案

    我一直在浏览LLVM并遇到以下lines:

    // Decide whether to use availability macros.
    #if !defined(_LIBCPP_BUILDING_LIBRARY) &&                                      \
        !defined(_LIBCPP_DISABLE_AVAILABILITY) &&                                  \
        __has_feature(attribute_availability_with_strict) &&                       \
        __has_feature(attribute_availability_in_templates)
    #  ifdef __APPLE__
    #    define _LIBCPP_USE_AVAILABILITY_APPLE
    #  endif
    #endif
    
    因此,我将-D_LIBCPP_DISABLE_AVAILABILITY传递给了编译器,它起作用了!我怀疑这是否是有效的解决方案,但它可能会对某人有所帮助。另一个选择可能是坚持使用https://github.com/gulrak/filesystem,它基本上是std::filesystem,但是在标准库之外。
    回答您的一些问题:

    嗯,这不是错误,但是是的,这是LLVM的功能,而且它似乎是专门为Apple设计的。

    不,这不是Macports of Homebrew的错误。

    如您所见,该可用性功能在源代码中,因此看来从源代码中使用它还是使用包管理器都没关系。

    不幸的是,我在这里无法声明任何内容,但是应该这样做是有原因的。

    关于c++ - llvm 9是否在10.15之前的Mac版本上支持std::filesystem?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58648316/

    10-14 19:38
    查看更多