我有一台运行 El Capitan 版本 10.11.6 的旧 Mac(大约 2009 年)。Apple 不允许在我的机器上进一步更新其操作系统,但通过 Macports,它可以作为非 Apple 特定软件的良好开发环境。
我正在使用 g++ 9.2 进行编译,它支持开箱即用的 std::filesystem,而使用不支持的 clang 8.0。(在每种情况下都使用每个编译器的本机标准库。)我正在使用 --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 和操作系统版本之间引入依赖关系。
- 这是 Macports 的错误吗?也许他们在构建时没有使用正确的标志?
- 如果我要本地构建 LLVM 和 Clang,我可以解决这个问题吗?
- 这是一个问题吗?也许 LLVM 的好人知道一些 GCC 的好人不知道的事情。
注意:有一个类似的问题涉及通过 Homebrew 下载的 clang/llvm。不幸的是,评论没有帮助。 [LLVM-9 clang-9 OSX]:std::filesystem::path 无法识别