Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::filesystem::directory_iterator linker issue (C++17) [duplicate]

Tags:

c++

c++17

I'm having an issue with my C++ built when trying to use the std::filesystem::directory_iterator from the C++17 standard.

Here is the code:

std::vector<std::string> IO::getDirectoryList(std::filesystem::path& dirPath)
{
  std::vector<std::string> files;
  for (auto& file : std::filesystem::directory_iterator("."))
  {
    files.push_back(file.path());
  }
  return files;
}

I get the following error:

> In function
> `StoryTime::IO::getDirectoryList(std::filesystem::__cxx11::path&)':
> IO.cpp:(.text+0x122): undefined reference to
> `std::filesystem::__cxx11::directory_iterator::operator*() const'
> IO.cpp:(.text+0x178): undefined reference to
> `std::filesystem::__cxx11::directory_iterator::operator++()'
> CMakeFiles/StoryTime.dir/IO.cpp.o: In function
> `std::filesystem::__cxx11::directory_iterator::directory_iterator(std::filesystem::__cxx11::path
> const&)':
> IO.cpp:(.text._ZNSt10filesystem7__cxx1118directory_iteratorC2ERKNS0_4pathE[_ZNSt10filesystem7__cxx1118directory_iteratorC5ERKNS0_4pathE]+0x26):
> undefined reference to
> `std::filesystem::__cxx11::directory_iterator::directory_iterator(std::filesystem::__cxx11::path
> const&, std::filesystem::directory_options, std::error_code*)'
> CMakeFiles/StoryTime.dir/IO.cpp.o: In function
> `std::filesystem::__cxx11::path::path<char [2],
> std::filesystem::__cxx11::path>(char const (&) [2],
> std::filesystem::__cxx11::path::format)':
> IO.cpp:(.text._ZNSt10filesystem7__cxx114pathC2IA2_cS1_EERKT_NS1_6formatE[_ZNSt10filesystem7__cxx114pathC5IA2_cS1_EERKT_NS1_6formatE]+0x6d):
> undefined reference to
> `std::filesystem::__cxx11::path::_M_split_cmpts()' collect2: error: ld
> returned 1 exit status

This is using CMake-3.8 with gcc-8/g++-8 which should have fine support for std::filesystem.

This is the output of c++ -v:

> Using built-in specs. COLLECT_GCC=c++
> COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/8/lto-wrapper
> OFFLOAD_TARGET_NAMES=nvptx-none OFFLOAD_TARGET_DEFAULT=1 Target:
> x86_64-linux-gnu Configured with: ../src/configure -v
> --with-pkgversion='Ubuntu 8.1.0-5ubuntu1~16.04' --with-bugurl=file:///usr/share/doc/gcc-8/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-8 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 8.1.0 (Ubuntu 8.1.0-5ubuntu1~16.04)

And CMake finds the right compiler:

> -- The C compiler identification is GNU 8.1.0
> -- The CXX compiler identification is GNU 8.1.0
> -- Check for working C compiler: /usr/bin/cc
> -- Check for working C compiler: /usr/bin/cc -- works
> -- Detecting C compiler ABI info
> -- Detecting C compiler ABI info - done
> -- Detecting C compile features
> -- Detecting C compile features - done
> -- Check for working CXX compiler: /usr/bin/c++
> -- Check for working CXX compiler: /usr/bin/c++ -- works
> -- Detecting CXX compiler ABI info
> -- Detecting CXX compiler ABI info - done
> -- Detecting CXX compile features
> -- Detecting CXX compile features - done
> -- Configuring done
> -- Generating done
> -- Build files have been written to:

Note that just compiling with the filesystem header works and also with code using std::filesystem::path. But once I try and use std::filesystem::directory_iterator the linker issue appears.

Any help is appreciated.

like image 218
Rasmus Dall Avatar asked Jul 09 '18 19:07

Rasmus Dall


1 Answers

Per this reddit post by u/forcecharlie you need to add -lstdc++fs to the compiler options. On Wandbox if we don't add it we get link errors but when we do add it, it compiles successfully.

like image 196
NathanOliver Avatar answered Nov 08 '22 19:11

NathanOliver