Can <filesystem> and respectively <experimental/filesystem> be used for both Windows and Linux?

1k Views Asked by At

I am looking for a cross-platform (nothing weird, standard Linux and Windows installations for desktop) way of working with directories and files (example: list the contents of a directory, check if a path is a file or directory etc.). I don't want to use any boost, Qt etc.

So after some research I found out about the <filesystem> header. Since I use C++14 I checked and found out that pre-standard implementation (the filesystem functionality became part of the C++ standard with C++17) it can be found (or at least so far the parts that I use) as <experimental/filesystem>.

My knowledge in terms of Windows and Visual C++ are quite lacking so my question is does this apply also to it or just GCC and Clang (the ones I've tried so far)? I know that when using cmake I need to distinguish between Clang and GCC when linking (see also this bug report from 3 years ago):

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
  target_link_libraries(${PROJECT_NAME} c++experimental)
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  target_link_libraries(${PROJECT_NAME} stdc++fs)
endif()

I don't know how to handle Visual C++ in this case. My project needs to be as portable as possible given a specific standard for C++.


NOTE: I don't want to use C++17 but I want to add the possibility for a support for it in my code IF someone builds it with that standard enabled. That is why distinguishing between C++14 and C++17 is important.

1

There are 1 best solutions below

1
On

You'd better not.

From MSVC STL source:

The <experimental/filesystem> header providing std::experimental::filesystem is deprecated by Microsoft and will be REMOVED. It is superseded by the C++17 header providing std::filesystem.

So it will be an issue with future Visual Studio versions.

I guess the best option is to switch to C++17.

Common alternative is <boost/filesystem>. Less common alternative is another cross-platform library, such as Qt.