Xcode OSX 上的 C++ 构建失败并出现多个错误文件 IO ... 不可用:在 macOS 10.15 中引入

C++ Build Failed on Xcode OSX with multiple errors File IO ... is unavailable: introduced in macOS 10.15

每当我尝试在 Mac.Xcode 上构建代码时,我都会收到以下错误。

我当前的系统:
macOS:版本 10.15.1 (19B88)
Xcode:版本 11.2.1 (11B500)

我的错误:

'path' is unavailable: introduced in macOS 10.15
'current_path' is unavailable: introduced in macOS 10.15
'operator/' is unavailable: introduced in macOS 10.15
'path' is unavailable: introduced in macOS 10.15
'path' is unavailable: introduced in macOS 10.15

main.cpp

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <filesystem>

using namespace std;

int main(int argc, char* argv[])
{
    cout << "being exectued from:" << endl;
    cout << argv[0] << endl;

    std::__fs::filesystem::path cwd = std::__fs::filesystem::current_path() / "filename.txt"; // C++17
    cout << "but the input.txt and output.txt files should be be in the following directory:" << endl;
    cout << cwd.string() << endl << endl;

在终端上 运行 g++ 之后我得到

clang: error: no input files

然后 运行 g++ --version 我得到

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1 Apple clang version 11.0.0 (clang-1100.0.33.12) Target: x86_64-apple-darwin19.0.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

使用 SDK 10.15、Xcode 11 和启用 C++17 编译器解决了这个问题。

要启用 C++17,请遵循此 link:Enable C++17 on Xcode, macOS

在您的 Xcode 上,从常规设置中,select 10.15 SDK 作为 部署目标 并且您是很适合。

添加

CONFIG += c++17

macx: {
    QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.15
}

到您的 .pro 文件。 那应该为编译器设置 -std=gnu++1z -mmacosx-version-min=10.15 标志。 我还为 C++ 和 C 使用了 Apple Clang 编译器(可以在 Preferences -> Kits 中设置)。不确定是否关键,但 GCC 确实无法正常工作。