本文介绍了带有c ++ 17的`filesystem`在我的mac os x high sierra上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习本教程:

这是我用于 CLion 的 CMakeLists.txt 文件:

cmake_minimum_required(版本 3.8)项目(文件系统2)设置(CMAKE_CXX_STANDARD 17)add_executable(FileSystem2 main.cpp)

这是 > 的输出.gxx --version:

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

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

编辑

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

解决方案

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 install --with-toolchain llvm # llvm 但包含所有标题xcode-select --install # 安装您可能正在使用的附加标头.echo 'export PATH="/usr/local/opt/llvm/bin:$PATH"' >>~/.bash_profile # 将自定义 llvm 路径导出到 shellsudo ln -s/usr/local/opt/llvm/bin/clang++/usr/local/bin/clang++-brew # 可选,但我喜欢设置符号链接.

编辑 2:

Clang 6.0 还没有在 macOS 上包含 ,但是您可以获取 ,并链接到 -lc++experimental,并使用 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++实验性

编辑 3:

当前 clang 版本 7.0.1 支持 .无论如何,编译器命令行一定会略有不同:

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.

I'm following this tutorial:

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

to checkout new c++ filesystem feature. However I'm unable to compile even minimal example on my machine:

#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;
}

I was using XCode, CLion and command line while trying to compile it but nothing works, I have Version 9.3 (9E145) with (seemingly proper) project settings, none of which work:

Here is my CMakeLists.txt file for CLion:

cmake_minimum_required(VERSION 3.8)

project(FileSystem2)

set(CMAKE_CXX_STANDARD 17)

add_executable(FileSystem2 main.cpp)

Here is an output from > gxx --version:

Nevertheless this is what I get as an output from my IDEs:

What am I doing wrong, it looks to me that my compiler should support c++17?


Edit

As per Owen Morgan's answer I've installed clang (actual install command was brew install llvm) but it now complains about absence of string.h. Any thoughts?

解决方案

The compiler shipped with Xcode supports C++17 language features but not C++17 standard library features. Looking at your screenshot you will see the standard library support goes up to C++11, and Apple has not yet shipped a version of clang that has stdlib support for either C++14 or C++17.

However hope is not lost! You can download the newest version of clang from the brew package manager.

brew install clang

Then you can compile by setting your cmake compiler flags to be your custom brew version and then running that.

Here is a link how to do that: http://antonmenshov.com/2017/09/09/clang-openmp-setup-in-xcode/

Edit:

After installing llvm you will need to link your llvm path into your current shell. I have a shell script that I use at work to get this set up properly. Hope this helps.

#!/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.

Edit 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 instead of std::filesystem.

Final command line invocation:

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

Edit 3:

Current clang version 7.0.1 supports either <filesystem> or <experimental/filesystem>. In any case, the compiler command line must be slightly different:

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

with -lc++fs instead of -lc++experimental.Optionally, you can replace-std=c++1z with -std=c++17 as well.

这篇关于带有c ++ 17的`filesystem`在我的mac os x high sierra上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 07:38