我有一台运行El Capitan 10.11.6版的旧Mac(大约在2009年)。 Apple不允许在我的计算机上对其操作系统进行进一步的更新,但是通过Macports,它可以为非Apple专用软件提供良好的开发环境。
我正在使用现成的支持std :: filesystem的g ++ 9.2以及不支持的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是上述错误消息的最终原因。
所以,我的问题是:
注意:还有一个类似的问题,涉及通过Homebrew下载的clang / llvm。不幸的是,评论没有帮助。 [LLVM-9 clang-9 OSX]: std::filesystem::path unrecognized
答案 0 :(得分:1)
我一直在浏览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的错误吗?毕竟,GCC不会在std :: filesystem和OS版本之间引入依赖关系。
嗯,这不是错误,但是是的,这是LLVM的功能,而且似乎是为Apple设计的。
这是Macports的错误吗?也许他们在构建时没有使用正确的标志?
不,这不是Macports of Homebrew的错误。
如果我本机构建LLVM和Clang,我可以解决此问题吗?
如您所见,此可用性功能已在源代码中提供,因此似乎从源代码或使用包管理器进行构建都没有关系。
这是一个问题吗?也许LLVM的好人知道GCC的好人所不知道的东西。
不幸的是,我在这里无法声明任何内容,但这应该是有原因的。