Bačić
Bačić

Reputation: 61

How to enable filesystem.h or fix experimental/filesystem?

I must write code for filesystem for my project. I am using Linux and Clion for my work. I am include and try to compile simple code for but there is error which said there is no reference to std::experimental::filesystem::create:directories. Compiler is c++ by default(I tried gcc but there is errors too). I can't include only and I dont have idea what to do next. Can anyone help me?

I expect created directory via std::experimental::filesystem::create_directory("root)

Upvotes: 3

Views: 5853

Answers (2)

Bačić
Bačić

Reputation: 61

I fix the problem. For all who reading this question, in CMakeLists.txt you must link libraries BEFORE everything else. CMakeLists.txt must be like:

cmake_minimum_required(VERSION 3.14)
project(FileSystem)

set(CMAKE_CXX_STANDARD 17)
link_libraries(-lstdc++fs)
add_executable(FileSystem main.cpp Superblok.cpp Superblok.h Inode.cpp Inode.h 
Memory.cpp Memory.h Filesystem.cpp Filesystem.h)

if you put link_libraries(-lstdc++fs) in the end,your code wont work.

Upvotes: 0

rubenvb
rubenvb

Reputation: 76519

Depending on your compiler, its standard library and its versions you need to link with the separate filesystem library:

  • GCC from 6.x and 7.x: #include <experimental/filesystem> gives you std::experimental::filesystem and you must then link with -lstdc++fs
  • GCC 8.x and newer: #include <filesystem> gives you std::filesystem and you must then link with -lstdc++fs
  • Clang/libc++ 5.x to 6.x: #include <experimental/filesystem> gives you std::experimental::filesystem and you must then link with -lc++experimental.
  • Clang/libc++ 7.x and newer: #include <filesystem> gives you std::filesystem and you must then link with -lc++fs.

Note you'll need libc++ built explicitly with

-DLIBCXX_INSTALL_EXPERIMENTAL_LIBRARY=YES

and/or

-DLIBCXX_INSTALL_FILESYSTEM_LIBRARY=YES

Upvotes: 4

Related Questions