macOS Clang C++17 filesystem header not found

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

macOS Clang C++17 filesystem header not found



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


C++17


clang



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


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 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 gcc but seems to try to compile for iOS leading to another error which seems to be related to iostream


gcc


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.





If the assignment has a requirement for this, perhaps the instructor should have told you how to actually achieve this (compiler and compiler flags)?
– crashmstr
Mar 6 '17 at 19:48





Well he only uses Linux so he didn't know how to do it on a mac...
– snoato
Mar 6 '17 at 19:59





@DeiDei Libc++ (Clang) will not ship <filesystem> in 4.0.
– EricWF
Mar 6 '17 at 20:22


<filesystem>





If you really have to do this with C++17 then either install a Linux VM on your Mac or see whether something like homebrew has C++17 + filesystem support yet.
– Paul R
Mar 6 '17 at 22:14





For the gcc case this may help
– Shafik Yaghmour
Mar 6 '17 at 22:18




5 Answers
5



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


<experimental/filesystem>


<filesystem>



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


<filesystem>





I see, thank you :) But shouldn't it be able to include <experimental/filesystem>?
– snoato
Mar 7 '17 at 7:59





The rate at which Apple ships upstream libc++ changes is not up to me. Libc++ first shipped <experimental/filesystem> in 3.9 but Apple has yet to release it as a part of XCode. If you want to build your own version of libc++ from trunk it will have <experimental/filesystem>.
– EricWF
Mar 7 '17 at 9:01



<experimental/filesystem>


<experimental/filesystem>





Including <experimental/filesystem> 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?
– Quuxplusone
Sep 7 '17 at 22:34


<experimental/filesystem>


-lstdc++fs


I don't know





Its a shame that filesystem is still not useable because Xcode is missing it and boost also misses some features from the specification (the charset u8string conversions for example). All we can do is wait for xcode 9.1 or later
– Lothar
Sep 21 '17 at 2:00





Just installed Xcode 9.2 and still no <experimental/filesystem> :(
– Rudolfs Bundulis
Dec 15 '17 at 18:31


<experimental/filesystem>



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?



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


-lc++experimental



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



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>".



The release notes also do not mention libc++17 <filesystem>. The release notes do mention that the following are in Xcode 10: <any>, <optional>, and <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



Installed Xcode 9.4 - no



but homebrew came to the rescue with llvm 6



brew update
brew install llvm



and with a change in PATH, I was away.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

Executable numpy error

Trying to Print Gridster Items to PDF without overlapping contents

Hystrix command on request collapser fallback