Cur*_*ous 5 c++ gcc llvm clang std-filesystem
我有一台运行 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';
}
Run Code Online (Sandbox Code Playgroud)
这是 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")
Run Code Online (Sandbox Code Playgroud)
这是编译器的抱怨:
[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 {
...
Run Code Online (Sandbox Code Playgroud)
向后工作,我在 /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)))
Run Code Online (Sandbox Code Playgroud)
据我所知,这个 #define 是上述错误消息的最终原因。
所以,我的问题是:
注意:有一个类似的问题涉及通过 Homebrew 下载的 clang/llvm。不幸的是,评论没有帮助。 [LLVM-9 clang-9 OSX]: std::filesystem::path 无法识别
小智 1
我一直在浏览 LLVM 并遇到以下几行:
// 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
Run Code Online (Sandbox Code Playgroud)
所以我传递-D_LIBCPP_DISABLE_AVAILABILITY给编译器并且它起作用了!我怀疑这是否是一个有效的解决方案,但它可能会对某人有所帮助。另一种选择可能是坚持使用https://github.com/gulrak/filesystem,它基本上是std::filesystem,但在标准库之外。
回答您的一些问题:
这是 LLVM 的错误吗?毕竟,GCC 不会在 std::filesystem 和操作系统版本之间引入依赖关系。
嗯,这不是一个错误,但是,是的,这是 LLVM 的一项功能,而且似乎是专门为 Apple 设计的。
这是 Macports 的错误吗?也许他们在构建时没有使用正确的标志?
不,这不是 Macports 或 Homebrew 的错误。
如果我要原生构建 LLVM 和 Clang,我可以解决这个问题吗?
正如您所看到的,此可用性功能位于源代码中,因此似乎从源代码构建它或使用包管理器构建它并不重要。
这是一个问题吗?也许 LLVM 的专家知道一些 GCC 的专家不知道的事情。
不幸的是,我不能在这里说明任何事情,但这样做应该是有原因的。
| 归档时间: |
|
| 查看次数: |
268 次 |
| 最近记录: |