52

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.

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

When I try to compile a sample program that just includes filesystem and iostream (and writes to 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

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.

snoato
  • 644
  • 1
  • 5
  • 12
  • 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 06 '17 at 19:48
  • 1
    Well he only uses Linux so he didn't know how to do it on a mac... – snoato Mar 06 '17 at 19:59
  • @DeiDei Libc++ (Clang) will not ship `` in 4.0. – EricWF Mar 06 '17 at 20:22
  • 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](http://brew.sh) has C++17 + filesystem support yet. – Paul R Mar 06 '17 at 22:14
  • For the gcc case [this may help](http://stackoverflow.com/a/30103992/1708801) – Shafik Yaghmour Mar 06 '17 at 22:18
  • This question was asked *just* after the last changes were made for C++17, but it’s not the C++17 filesystem library if it’s experimental (or `std::experimental`). – Davis Herring Nov 21 '19 at 03:36

8 Answers8

27

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

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

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
EricWF
  • 1,005
  • 7
  • 8
  • 4
    I see, thank you :) But shouldn't it be able to include ? – snoato Mar 07 '17 at 07:59
  • 2
    The rate at which Apple ships upstream libc++ changes is not up to me. Libc++ first shipped `` 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 ``. – EricWF Mar 07 '17 at 09:01
  • 5
    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? – Quuxplusone Sep 07 '17 at 22:34
  • 3
    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 02:00
  • 14
    Just installed Xcode 9.2 and still no `` :( – Rudolfs Bundulis Dec 15 '17 at 18:31
17

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

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::filesystem for projects targeting these versions or later.

Community
  • 1
  • 1
Brad Allred
  • 7,323
  • 1
  • 30
  • 49
  • Is it possible being supported ONLY on the latest OS versions is related to Xcode 11 being in Beta? That would seem unworkable if you intended to support MacOS 10.12 through 10.14. Isn't there also Boost support built into earlier versions of MacOS? Perhaps the thinking is that we call all just use Boost.Filesystem before 10.15? – SMGreenfield Jun 04 '19 at 18:31
  • @SMGreenfield my suspicion is that `libc++` on versions of macOS prior to 10.15 are lacking the filesystem implementation altogether. If you were to statically link `libc++` its _possible_ you could run your application on older systems (but there might be other issues with doing that depending on implementation). AFIK boost does not ship with macOS, but there is nothing stopping you from building/shipping with it. – Brad Allred Jun 04 '19 at 20:03
  • I am on Xcode Version 11.2.1 (11B500) and there's no `filesystem`. – I. Antonov Nov 20 '19 at 06:26
  • 1
    @I.Antonov I assure you it exists; I've been using it. Be sure to enable c++17 mode in your project. You may also need to change your SDK to one of the versions listed in my answer. – Brad Allred Nov 20 '19 at 06:38
  • 1
    @BradAllred Hi, I went to Build Settings and then selected `C++17[-std=c++17]` from the `C++ Language Dialect` and still same. – I. Antonov Nov 20 '19 at 07:50
  • @BradAllred if possible could you please check this thread and let me know where and what i'm doing wrong? I'd appreciate the help. https://stackoverflow.com/questions/58943032/c-build-failed-on-x-code-osx-with-multiple-errors-file-io-is-unavailab?noredirect=1#comment104144855_58943032 – I. Antonov Nov 20 '19 at 07:51
  • @I.Antonov I also said you need to use one of the supported SDKs. – Brad Allred Nov 20 '19 at 17:04
9

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

Jason
  • 336
  • 2
  • 5
5

EDIT

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

Clang now supports the C++17 <filesystem> library for iOS 13, macOS 10.15, watchOS 6, and tvOS 13. (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/.

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:

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

bfx
  • 897
  • 10
  • 16
3

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

jvillasante
  • 89
  • 1
  • 5
1

I installed XCode 9.4 - no <filesystem>

But Homebrew came to the rescue with LLVM 6

brew update
brew install llvm

And with a change in PATH, I was away.

Parsa
  • 997
  • 1
  • 15
  • 38
DeeDeeCee
  • 21
  • 1
1

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

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

Max Raskin
  • 1,042
  • 1
  • 14
  • 26
0

Recursive directory walk using ftw in c, more details here.

On, -std=c++17 for macOS version 10.xx, filesystem header is not available.

#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;
}

output looks like following:

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
Milind Deore
  • 2,887
  • 5
  • 25
  • 40