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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42633477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:30:17  来源:igfitidea点击:

macOS Clang C++17 filesystem header not found

c++macosgccclangc++17

提问by snoato

I need to write a program using the (experimental) C++17filesystem library but clangon my Mac (macOS 10.12.03)doesn't seem to have the filesystem header included.

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

Since I'm required to use the C++17, I cannot use alternatives like the Boostlibrary.

由于我需要使用C++17,我不能使用像Boost库这样的替代品。

When I try to compile a sample program that just includes filesystem and iostream(and writes to cout)

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

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

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

I get the following error message:

我收到以下错误消息:

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

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

When I try the same using GCC 6.3 (installed via homebrew) I get:

当我尝试使用 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.

I also tried using experimental/filesystem instead which compiles using gccbut seems to try to compile for iOS leading to another error which seems to be related to iostream

我也尝试使用实验/文件系统代替它编译使用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

The version of my clang is:

我的叮当版本是:

>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

I'm grateful for any helpful input since I couldn't find anything that solved my problem so far (although I might have been searching for the wrong terms).

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

If you need more information I'll gladly provide it but I hope to have included everything.

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

采纳答案by EricWF

Libc++, which is the C++ standard library on OS X, has not moved <experimental/filesystem>to <filesystem>yet because the specification is not stable.

Libc++ 是 OS X 上的 C++ 标准库,由于规范不稳定<experimental/filesystem><filesystem>尚未迁移。

Hopefully <filesystem>will be a part of the Clang 6.0 release. (We missed 5.0)

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

回答by Brad Allred

Xcode 11 Beta now includes <filesystem>. Unlike the other answers indicating beta support in Xcode 10, Apple has mentioned this in the release notes.

Xcode 11 Beta 现在包含<filesystem>. 与其他表示 Xcode 10 支持 beta 的答案不同,Apple 在发行说明中提到了这一点。

Also mentioned in the release notes, is this is only supported by iOS 13, macOS 10.15, watchOS 6, and tvOS 13. You will only be able to use std::filesystemfor projects targeting these versions or later.

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

回答by Jason

In reply to Max Raskin: I've installed Xcode 10 Beta 4, from July 17, 2018, and this version does not have "#include <experimental/filesystem>" or "#include <filesystem>".

回复 Max Raskin:我已经安装了 Xcode 10 Beta 4,从 2018 年 7 月 17 日开始,这个版本没有“#include <experimental/filesystem>”或“#include <filesystem>”。

The release notesalso do not mention libc++17 <filesystem>. The release notes domention that the following are in Xcode 10: <any>, <optional>, and <variant>.

发行说明也没有提到的libc ++ 17的<filesystem>。发行说明确实提到以下内容在 Xcode 10 中:<any>、<optional> 和 <variant>。

Example include file location:

示例包含文件位置:

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

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

回答by bfx

EDIT

编辑

As mentioned in another answer <filesystem>is available in Xcode 11 Beta according to the release notes:

<filesystem>根据发行说明,Xcode 11 Beta 中提供了另一个答案:

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

Clang 现在支持<filesystem>适用于 iOS 13、macOS 10.15、watchOS 6 和 tvOS 13的 C++17库。(50988273)

Here's hoping it's meant to stay this time!

希望这次能留下来!

OLD ANSWER

旧答案

Just checked Xcode 10.2 Beta 4 and it has regular <filesystem>! For the curious, it's in /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/.

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

EDIT:

编辑:

Downloaded Xcode 10.2 (10E125) aaaaand ... <filesystem>is gone again. No mention whatsoever in the release notes. If you happen to have an Xcode version that contains <filesystem>lying around (like the Beta 4 I mentioned earlier) copying the file over seems to work okay:

下载的 Xcode 10.2 (10E125) aaaaand ...<filesystem>又不见了。发行说明中没有提及任何内容。如果您碰巧有一个 Xcode 版本,其中包含<filesystem>(如我之前提到的 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/

Mind you, of course, every Xcode update will very likely break this workaround and make another copy necessary. Also, there is probably a good reason why the beta implementation didn't make it into the release. Proceed with caution...

请注意,当然,每次 Xcode 更新很可能会破坏此解决方法并需要另一个副本。此外,可能有一个很好的理由为什么 beta 实现没有进入 release。谨慎行事...

回答by jvillasante

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?

包括让您获得声明,但要获得定义,您还必须与 -lstdc++fs(对于 libstdc++)或我不知道(对于 libc++)进行链接。如果有人知道,也许他们可以更新这个答案?

For libc++ you need to link with -lc++experimental

对于 libc++,您需要链接 -lc++experimental

回答by DeeDeeCee

Installed Xcode 9.4 - no

已安装 Xcode 9.4 - 否

but homebrew came to the rescue with llvm 6

但是自制软件用 llvm 6 来拯救

brew update brew install llvm

brew 更新 brew 安装 llvm

and with a change in PATH, I was away.

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

回答by Max Raskin

If anyone still interested, Xcode 10 Beta ships with libc++ that has experimental/filesystem

如果还有人感兴趣,Xcode 10 Beta 附带具有实验性/文件系统的 libc++

UPDATEone of Xcode 10 betas used to ship with it, perhaps by accident, Xcode 10.1 unfortunately, doesn't have it :(

更新用于随附的 Xcode 10 测试版之一,也许是意外,不幸的是,Xcode 10.1 没有它:(