我在Ubuntu 16.04上使用gcc 7.2,我需要使用C++ 17中的新文件系统库.即使确实有一个名为experimental/filesystem的库,我也不能使用它的任何成员.例如,当我尝试编译此文件时:
#include <iostream>
#include <string>
#include <experimental/filesystem>
using namespace std;
namespace fs = std::experimental::filesystem::v1;
int main(){
fs::path p1 = "/usr/share/";
}
Run Code Online (Sandbox Code Playgroud)
我收到一个编译错误,如下所示:
$ g++-7 test.cpp -std=c++17
/tmp/ccfsMnlG.o: In function `std::experimental::filesystem::v1::__cxx11::path::path<char [12], std::experimental::filesystem::v1::__cxx11::path>(char const (&) [12])':
test.cpp:(.text._ZNSt12experimental10filesystem2v17__cxx114pathC2IA12_cS3_EERKT_[_ZNSt12experimental10filesystem2v17__cxx114pathC5IA12_cS3_EERKT_]+0x73): undefined reference to `st
d::experimental::filesystem::v1::__cxx11::path::_M_split_cmpts()'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我不认为代码有任何问题,因为我只是从网站上复制粘贴它.我使用的是错误的gcc版本吗?另外,为什么我需要<experimental/filesystem>
而不仅仅是<filesystem>
在C++ 17中呢?提前致谢.
我试图推出新的filesystem
STL库,但由于某种原因我遇到了错误.该Clang++7
网站表明它应该支持新的filesystem
图书馆 - 事实上我clang
已经超前了g++
.
我使用了另一个Stack Exchange帖子中的一些代码,因此根据upvotes的数量它应该是有效的.这可能会转到指定的目录并打印该目录中的所有文件.这是代码.
#include <iostream>
#include <string>
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
int main(int argc, char *argv[])
{
std::string path = "/home/.../Downloads";
for (const auto & entry : fs::directory_iterator(path))
{
std::cout << entry.path() << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误消息是:
CMakeFiles/filesystem_app.dir/main.cpp.o: In function `main':
/media/.../clangcpp/filesystem_app/main.cpp:13: undefined reference to `std::experimental::filesystem::v1::__cxx11::directory_iterator::operator*() const'
/media/.../clangcpp/filesystem_app/main.cpp:13: undefined reference to `std::experimental::filesystem::v1::__cxx11::directory_iterator::operator++()'
CMakeFiles/filesystem_app.dir/main.cpp.o: In function `path<std::__cxx11::basic_string<char>, std::experimental::filesystem::v1::__cxx11::path>':
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.5.0/../../../../include/c++/5.5.0/experimental/fs_path.h:198: undefined reference to `std::experimental::filesystem::v1::__cxx11::path::_M_split_cmpts()'
CMakeFiles/filesystem_app.dir/main.cpp.o: In function `directory_iterator': …
Run Code Online (Sandbox Code Playgroud)