找不到 macOS Clang C++17 文件系统 header

macOS Clang C++17 filesystem header not found

我需要使用(实验性的)C++17 文件系统库编写程序,但 clang 在我的 Mac (macOS 10.12.03) 似乎没有包含文件系统 header。

因为我需要使用 C++17,所以我不能使用像 Boost 库这样的替代品。

当我尝试编译仅包含文件系统和 iostream(并写入 cout)的示例程序时

#include <filesystem>
#include <iostream>
using namespace std;

int main(){
    cout << "test" << endl;
}

我收到以下错误消息:

>clang test.cpp -std=c++1z

test.cpp:2:10: fatal error: 'filesystem' file not found
#include <filesystem>
         ^
1 error generated.

当我使用 GCC 6.3(通过自制软件安装)尝试相同操作时,我得到:

>gcc-6 test.cpp  -std=c++17 
test.cpp:2:22: fatal error: filesystem: No such file or directory
 #include <filesystem>
                      ^
compilation terminated.

我也尝试使用 experimental/filesystem 而不是使用 gcc 进行编译,但似乎尝试为 iOS 进行编译导致另一个似乎与 iostream 有关的错误

Undefined symbols for architecture x86_64:
  "std::ios_base::Init::Init()", referenced from:
      __static_initialization_and_destruction_0(int, int) in ccd5QiVt.o
  "std::ios_base::Init::~Init()", referenced from:
      __static_initialization_and_destruction_0(int, int) in ccd5QiVt.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

我的 clang 版本是:

>clang --version
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin16.4.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

我很感激任何有用的输入,因为到目前为止我找不到任何解决我问题的方法(尽管我可能一直在搜索错误的术语)。

如果您需要更多信息,我很乐意提供,但我希望已包括所有内容。

Libc++,它是 OS X 上的 C++ 标准库,尚未将 <experimental/filesystem> 移动到 <filesystem>,因为规范不稳定。

希望 <filesystem> 将成为 Clang 6.0 版本的一部分。 (我们错过了 5.0)

Including gets you the declarations, but to get the definitions you also have to link with -lstdc++fs (for libstdc++) or I don't know (for libc++). If someone knows, maybe they could update this answer?

对于 libc++,您需要 link 和 -lc++experimental

我安装了 XCode 9.4 - 没有 <filesystem>

但是 Homebrew 用 LLVM 6 来拯救

brew update
brew install llvm

随着 PATH 的变化,我离开了。

如果还有人感兴趣,Xcode 10 Beta 附带的 libc++ 具有 experimental/filesystem

UPDATE Xcode 10 个测试版之一,可能是偶然的,不幸的是,Xcode 10.1 没有它:(

回复 Max Raskin:我安装了 Xcode 10 Beta 4,从 2018 年 7 月 17 日开始,这个版本没有“#include ”或“#包括 <文件系统>".

release notes 也别提libc++17 。发行说明 do 提到以下内容在 Xcode 10 中:

示例包含文件位置:

/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/experimental

编辑

如另一个答案所述,<filesystem> 根据 release notes:

在 Xcode 11 Beta 中可用

Clang now supports the C++17 <filesystem> library for iOS 13, macOS 10.15, watchOS 6, and tvOS 13. (50988273)

希望这次能留下来!

旧答案

刚刚检查了 Xcode 10.2 Beta 4,它有常规 <filesystem>!对于好奇,它在 /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/.

编辑:

已下载 Xcode 10.2 (10E125) aaaaand ... <filesystem> 又不见了。 release notes 中没有任何提及。如果你碰巧有一个包含 <filesystem> 的 Xcode 版本(就像我之前提到的 Beta 4),复制文件似乎没问题:

$ sudo cp /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/filesystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/

请注意,当然,每个 Xcode 更新很可能会破坏此解决方法并需要另一个副本。此外,Beta 版实施未能进入发行版 可能是有充分理由的。谨慎行事...

Xcode 11 Beta 现在包括 <filesystem>。与 Xcode 10 中表示 Beta 支持的其他答案不同,Apple 在 release notes.

中提到了这一点

发行说明中还提到,这是否仅受 iOS 13、macOS 10.15、watchOS 6 和 tvOS 13 支持。您将只能将 std::filesystem 用于项目定位这些版本或更高版本。

在 c 中使用 ftw 递归目录遍历,更多详细信息 here

打开,-std=c++17 macOS 版本 10.xx,filesystem header 不可用。

#include <ftw.h>
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>

 
int list(const char *name, const struct stat *status, int type)
{
     if (type == FTW_NS)
     {
         return 0;
     }

     if (type == FTW_F)
     {
         printf("0%3o\t%s\n", status->st_mode&0777, name);
     }

     if (type == FTW_D && strcmp(".", name) != 0)
     {
         printf("0%3o\t%s/\n", status->st_mode&0777, name);
     }
     return 0;
}

int main(int argc, char *argv[])
{
     if(argc == 1)
     {
         ftw(".", list, 1);
     }
     else
     {
         ftw(argv[1], list, 1);
     }

     return 0;
}

输出如下所示:

0755    ./Shivaji/
0644    ./Shivaji/20200516_204454.png
0644    ./Shivaji/20200527_160408.png
0644    ./Shivaji/20200527_160352.png
0644    ./Shivaji/20200520_174754.png
0644    ./Shivaji/20200520_180103.png
0755    ./Saif/
0644    ./Saif/Snapchat-1751229005.jpg
0644    ./Saif/Snapchat-1356123194.jpg
0644    ./Saif/Snapchat-613911286.jpg
0644    ./Saif/Snapchat-107742096.jpg
0755    ./Milind/
0644    ./Milind/IMG_1828.JPG
0644    ./Milind/IMG_1839.JPG
0644    ./Milind/IMG_1825.JPG
0644    ./Milind/IMG_1831.JPG
0644    ./Milind/IMG_1840.JPG