24

如何在 OSX High Sierra (10.13.5) 上的 Xcode (9.4.1) 中启用 C++17?

4

2 回答 2

33

在 OSX High Sierra (10.13.5) 上的 Xcode (9.4.1) 中使用 C++17 的步骤:

  1. 在 Xcode 中打开现有的或创建新的 C++ 项目
  2. 单击“显示项目导航器”按钮。它位于 Xcode 窗口的左上角,就在最小化/最大化/关闭窗口按钮的下方。它是最左边的图标,看起来像一个文件夹。
  3. 单击“构建设置”并向下滚动以查找并展开“Apple LLVM 9.0 - 语言 - C++”部分
  4. 将 C++ 语言方言组合框选择更改为“C++17 [-std=c++17]”

Xcode 构建设置

验证步骤:

现在当我输出 __cplusplus 时,我看到 201703,并且我能够编译 C++17 功能,例如 if constexpr。

template<class T>
int compute(T x) {
    if constexpr( supportsAPI(T{}) ) {
        // only gets compiled if the condition is true
        return x.Method();
    } else {
        return 0;
    }
}

int main(){
    cout << __cplusplus << endl;
    return 0;
}

输出:

201703
Program ended with exit code: 0
于 2018-07-11T16:36:53.883 回答
5

在使用开发 CocoaPods(编写 C++ 库)时,我还必须更新podspec包含 c++ 17 代码的库,以编译包含此 pod 的主机应用程序。

所以我将这些标志添加到图书馆的podspec

  spec.xcconfig = { 
    "CLANG_CXX_LANGUAGE_STANDARD" => "c++17",
    "CLANG_CXX_LIBRARY" => "libc++"
  }
于 2020-08-03T16:30:18.870 回答