带有c 17的“文件系统”在我的mac os x high sierra上不起作用

新手上路,请多包涵

我正在关注本教程:

http://www.bfilipek.com/2017/08/cpp17-details-filesystem.html

检查新的 c++ filesystem 功能。但是我无法在我的机器上编译最小的例子:

 #include <string>
#include <iostream>
#include <filesystem>

namespace fs = std::filesystem;

int main()
{
    std::string path = "/";

    for (auto & p : fs::directory_iterator(path))
        std::cout << p << std::endl;
}

我在尝试编译时使用了 XCode、CLion 和命令行,但没有任何效果,我有 9.3 版(9E145)和(看似正确的)项目设置,但这些都不起作用:

在此处输入图像描述在此处输入图像描述

这是我的 CMakeLists.txt 文件:

 cmake_minimum_required(VERSION 3.8)

project(FileSystem2)

set(CMAKE_CXX_STANDARD 17)

add_executable(FileSystem2 main.cpp)

这是来自 > gxx --version 的输出:

在此处输入图像描述

尽管如此,这是我从 IDE 中得到的输出:

在此处输入图像描述在此处输入图像描述在此处输入图像描述

我做错了什么,在我看来我的编译器应该支持 c++17?


编辑

根据欧文摩根的回答,我已经安装了 clang (实际安装命令是 brew install llvm )但它现在抱怨缺少 string.h 。有什么想法吗?

原文由 Lu4 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 517
1 个回答

Xcode 附带的编译器支持 C++17 语言 特性,但不支持 C++17 标准库特性。查看您的屏幕截图,您会看到标准库支持上升到 C++11,Apple 尚未发布支持 C++14 或 C++17 的标准库的 clang 版本。

然而希望并没有消失!您可以从 brew 包管理器下载最新版本的 clang。

 brew install clang

然后,您可以通过将 cmake 编译器标志设置为您的自定义 brew 版本然后运行它来进行编译。

这是一个如何做到这一点的链接:http: //antonmenshov.com/2017/09/09/clang-openmp-setup-in-xcode/

编辑:

安装 llvm 后,您需要将 llvm 路径链接到当前 shell。我有一个 shell 脚本,我在工作中使用它来正确设置它。希望这可以帮助。

 #!/bin/bash
brew update
brew install --with-toolchain llvm # llvm but with all the headers
xcode-select --install # installs additional headers that you might be mimssing.
echo 'export PATH="/usr/local/opt/llvm/bin:$PATH"' >> ~/.bash_profile # exports the custom llvm path into the shell
sudo ln -s /usr/local/opt/llvm/bin/clang++ /usr/local/bin/clang++-brew # optional but I like to have a symlink set.

编辑2:

Clang 6.0 doesn’t have <filesystem> included on macOS yet, however you can get <experimental/filesystem> , and link against -lc++experimental , and use std::experimental::filesystem 而不是 std::filesystem

最终命令行调用:

Owen$ /usr/local/Cellar/llvm/6.0.0/bin/clang++ fs.cpp -std=c++1z -L /usr/local/Cellar/llvm/6.0.0/lib/ -lc++experimental

编辑3:

当前的 clang 版本 7.0.1 支持 <filesystem><experimental/filesystem> 。无论如何,编译器命令行必须略有不同:

 Owen$ /usr/local/Cellar/llvm/7.0.1/bin/clang++ main.cpp -std=c++1z -L /usr/local/Cellar/llvm/7.0.1/lib/ -lc++fs

-lc++fs 而不是 -lc++experimental 。或者,您也可以将 -std=c++1z 替换为 --- -std=c++17

原文由 Owen Morgan 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题