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.
As most other compilers clang provides some command line flags to control system header search explicitly. Most important of these is -isystem, which adds a directory to system include search path. Best way to ensure clangd can find your system includes is by putting the directories to be searched into your compile flags via -isystem.
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!
These headers are usually provided either by a custom toolchain, which might be part of the repository, or directly via system installed libraries. Clangd itself only ships with its own built-in headers, because they are tied to the version of clang embedded in clangd. The rest (including C++ STL) must be provided by your system.
The rest (including C++ STL) must be provided by your system. Clangd comes with an embedded clang parser. Hence it makes use of all the mechanisms that exist in clang for lookups, while adding some extra spices to increase chances of discovery on Mac environemnts.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With