作者:loy1231998 | 来源:互联网 | 2022-12-11 12:18
我在尝试使用std::filesystem::directory_iterator
C++ 17标准时构建了我的C++问题.
这是代码:
std::vector IO::getDirectoryList(std::filesystem::path& dirPath)
{
std::vector 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 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
.
这是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
链接器问题就出现了.
任何帮助表示赞赏.
1> NathanOliver..:
根据u/forcecharlie的reddit帖子,您需要添加到编译器选项中.在Wandbox上如果我们不添加它,我们会得到链接错误,但是当我们添加它时,它会成功编译.-lstdc++fs
这正是它.我建议在工具链文件或项目包含文件(用于cmake)中添加类似`link_libraries(stdc ++ fs)`的内容