std :: filesystem :: directory_iterator链接器问题(C ++ 17) [英] std::filesystem::directory_iterator linker issue (C++17)

查看:576
本文介绍了std :: filesystem :: directory_iterator链接器问题(C ++ 17)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用C ++ 17标准中的 std :: filesystem :: directory_iterator 时,我的C ++出现了问题。

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

这是代码:

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

这是将CMake-3.8与gcc-8 / g ++-8一起使用对 std :: filesystem 的很好支持。

This is using CMake-3.8 with gcc-8/g++-8 which should have fine support for 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找到正确的编译器:

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:

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

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.

我们将提供任何帮助。

推荐答案

每个此reddit帖子,由 u / forcecharlie 您需要在编译器选项中添加 -lstdc ++ fs 。如果我们不添加 Wandbox 我们收到链接错误,但是当我们添加链接错误时,编译成功

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.

这篇关于std :: filesystem :: directory_iterator链接器问题(C ++ 17)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆