【发布时间】:2018-12-16 22:27:35
【问题描述】:
我试图推出新的filesystem STL 库,但由于某种原因出现错误。 Clang++7 网站表明它应该支持新的filesystem 库——我相信clang 确实领先于g++。
我使用了另一个 Stack Exchange 帖子中的一些代码,因此它应该是有效的,基于赞成票的数量。这应该转到指定的目录并打印该目录中的所有文件。这是代码。
#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;
}
}
我收到的错误消息是:
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':
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.5.0/../../../../include/c++/5.5.0/experimental/fs_dir.h:188: undefined reference to `std::experimental::filesystem::v1::__cxx11::directory_iterator::directory_iterator(std::experimental::filesystem::v1::__cxx11::path const&, std::experimental::filesystem::v1::directory_options, std::error_code*)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我确保包含 experimental/filesystem 标头,而不仅仅是 filesystem,它删除了 Clion 中的所有红色曲线。我尝试从 CLion 以及命令行进行编译。我使用的编译字符串是:
clang++-7 -Wall -std=c++17 main.cpp -o app
有人知道这里出了什么问题吗?在编译错误消息中,我看到了对std::experimental::filesystem::v1::__cxx11::.. 的引用,我想知道为什么这没有说cxx17,但我不确定这是否是问题的原因。我在上面的编译字符串中明确指出了c++17。
【问题讨论】:
-
for clang >= 10 见stackoverflow.com/questions/33149878/…