如何在 gcc 8 上使用 std::filesystem?

2022-01-14 00:00:00 gcc namespaces c++

我已经更新了 gcc 版本,gcc --version 产生以下输出

I have updated version of gcc, gcc --version produces the following output

    gcc (Ubuntu 8.1.0-5ubuntu1~16.04) 8.1.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

我可以在头文件中包含文件系统而不会出现任何错误

i can include filesystem in header file without any error

#include<filesystem>

但是当我尝试像下面那样访问命名空间文件系统时,我得到了错误

But when i try to access the namespace filesystem like below then i get the error

namespace fs = std::filesystem;

错误信息

error: ‘filesystem’ is not a namespace-name
 namespace fs = std::filesystem;

这似乎很奇怪,因为 gcc 8 支持 std::filesystem 并且它在命名空间中不可用,我在访问 std::filesystem 时做错了吗?

This seems to be weird since the gcc 8 has support for std::filesystem and it is not available in namespace, am i doing anything wrong in accessing std::filesystem?

是的,我是用 -std=c++17 构建的

and yes i built with -std=c++17

推荐答案

将文件系统库添加为编译器的参数,该编译器将转发到链接器.还要确保您使用的是 C++17.g++ 和 clang++ 都接受这种特殊格式:

Add the filesystem library as an argument to your compiler that will be forwarded to the linker. Also make sure you are using C++17. Both g++ and clang++ accepts this particular format:

--std=c++17 -lstdc++fs

相关文章