首页 > 解决方案 > std::filesystem::directory_iterator 链接器问题 (C++17)

问题描述

我在尝试使用std::filesystem::directory_iterator来自 C++17 标准的 C++ 时遇到问题。

这是代码:

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;
}

我收到以下错误:

> 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

这是使用带有 gcc-8/g++-8 的 CMake-3.8,它应该对std::filesystem.

这是 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)

CMake 会找到正确的编译器:

> -- 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:

请注意,仅使用文件系统标头进行编译,也可以使用std::filesystem::path. 但是一旦我尝试使用std::filesystem::directory_iterator链接器问题就会出现。

任何帮助表示赞赏。

标签: c++c++17

解决方案


根据u/forcecharlie的这篇 reddit 帖子,您需要添加到编译器选项。在Wandbox 上,如果我们不添加它,我们会收到链接错误,但是当我们添加它时,它会成功编译-lstdc++fs


推荐阅读