C++17 支持 Eclipse Neon

C++17 support Eclipse Neon

我读到 here 虽然规范尚未完全准备好,但 C++17 功能完备。如何在我的代码中使用 C++17 功能,尤其是在 Eclipse CDT (Neon) 中?

具体来说,我想使用 filesystem 来轻松地遍历目录。

libc++ 和 libstdc++ 在最新版本中都有 std::experimental::filesystem。我不知道直接有 std::filesystem ; C++17 还没有发布,这似乎是合理的。

boostboost::filesystem,它们在一些方面有所不同,但结构几乎相同。使用 boost::filesystem 编写的代码可以相对容易地移植到 std::filesystem.

作为不兼容性的一个例子,boost 有一个单数标志枚举,而 std 有一个带有更多设置的复数标志枚举位域。

您可能需要将 -std=c++1z 传递给编译器,检查您的 libc++libstdc++ 版本,切换您正在使用的版本,安装新版本等。或者安装boost,并使用其基于 C++17 的文件系统库。

虽然 std::filesystem 计划随 C++17 一起提供,但当前的编译器实现尚未提供 "official" C++17 支持。由于 Yakk already stated in his answer,最近的编译器和标准 C++ 库版本有 std::experimental::filesystem.

至少对于GNU编译器来说g++我可以说你甚至不需要设置C++17语言方言,使用C++14就足够了!但是,您还需要 link(静态)库 libstdc++fs.a

此外,定义 std::filesystem 命名空间非常方便,这样您就可以使用 headers(几乎),就像它们已经完成一样:

// ...

#include <experimental/filesystem>
namespace std {
    namespace filesystem = std::experimental::filesystem;
}

// now use std::filesystem ...

总结:

  • 让eclipse使用c++1y(C++14)方言,这就够了
  • Link libstdc++fs.a
  • 包括<experimental/filesystem>