how to use std::filesystem on gcc 8?
Add the filesystem library as an argument to your compiler that will be forwarded to the linker. Also make sure you are using C++17. Both g++ and clang++ accepts this particular format:
--std=c++17 -lstdc++fs
Why does GCC not seem to have the filesystem standard library?
GCC v7
still does not implement <filesystem> but it does have the Filesystem Technical Specification which is in <experimental/filesystem>
#include <experimental/filesystem>
// for brevity
namespace fs = std::experimental::filesystem;
int main()
{
fs::path p = "/path/to/my/file"; // etc...
}
This is also available in GCC v6
.
To link with the library you need to add -lstdc++fs
to the command line.
Note: There may be some minor differences between the current Technical Specification and the final draft of <filesystem> that is decided upon by the Standards Committee.
Note 2: GCC v8
now implements <filesystem> with the -std=c++17
flag.
Link errors using filesystem members in C++17
Add the flag -lstdc++fs
:
$ g++-7 test.cpp -std=c++17 -lstdc++fs
gcc 7.2 supports C++17 experimental filesystem
namespace only. I do not know, maybe gcc 7.3 supports std filesystem
namespace already.
Problem adding std::filesystem to CMake Project
Besides from @lubgr's answer. I think a more complete way is to also do try_compile to see that you can actually use the filesystem header. This in my opinion is better because some compilers are not supporting std::filesystem yet. Also in gcc 7.x you have the filesystem under experimental
namespace. This way you can have a separate try_compile in the else
clause and detect that.
Here is the related cmake for it
# set everything up for c++ 17 features
set(CMAKE_CXX_STANDARD 17)
# Don't add this line if you will try_compile with boost.
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# test that filesystem header actually is there and works
try_compile(HAS_FS "${CMAKE_BINARY_DIR}/temp"
"${CMAKE_SOURCE_DIR}/tests/has_filesystem.cc"
CMAKE_FLAGS -DCMAKE_CXX_STANDARD=17 -DCMAKE_CXX_STANDARD_REQUIRED=ON
LINK_LIBRARIES stdc++fs)
if(HAS_FS)
message(STATUS "Compiler has filesystem support")
else()
# .... You could also try searching for boost::filesystem here.
message(FATAL_ERROR "Compiler is missing filesystem capabilities")
endif(HAS_FS)
The file tests/has_filesystem.cc is very simple
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
fs::path aPath {"../"};
return 0;
}
You could in your else clause try_compile for boost::filesystem and pass a directive that can be used in your source file where you decide if you want to use c++17 filesystem or boost.
What is the correct way to link C++17 filesystem with CMake?
It looks like there is no proper solution to this as of now. There still is an open issue on this subject on the CMake tracker.
Some seem to be using find modules like this one, which would allow you to use code like the following:
find_package(Filesystem REQUIRED)
add_executable(myapp myapp.cpp)
target_link_libraries(myapp PRIVATE std::filesystem)
In my opinion this is preferable to changing CMAKE_CXX_FLAGS
or linking against stdc++fs
directly.
Related Topics
Why Callback Functions Needs to Be Static When Declared in Class
How to Find a Search Term in Source Code
How to Set Breakpoints on Future Shared Libraries with a Command Flag
How to Avoid Code Duplication Implementing Const and Non-Const Iterators
Use Std::Fill to Populate Vector with Increasing Numbers
Call Base Class Method from Derived Class Object
What Exactly Is a 'Side-Effect' in C++
How to Set Visual Studio Filters for Nested Sub Directory Using Cmake
Macro Definition Containing #Include Directive
G++ How to Get Warning on Ignoring Function Return Value
Cross Platform Format String for Variables of Type Size_T
Pointer Interconvertibility VS Having the Same Address
Valgrind Memory Leak Errors When Using Pthread_Create
Linking with Clang++ on Os X Generates Lots of Symbol Not Found Errors
Std::Unique_Ptr, Deleters and the Win32 API
Avoiding Denormal Values in C++
How to Throttle the Bandwidth of a Socket Connection in C
Is There Support in C++/Stl for Sorting Objects by Attribute