26

How do I enable C++17 in Xcode (9.4.1) on OSX High Sierra (10.13.5)?

Phil Willoughby
  • 1,649
  • 12
  • 16
claytonjwong
  • 814
  • 1
  • 7
  • 13
  • 1
    What is the question? This look like an answer – NathanOliver Jul 11 '18 at 14:36
  • @claytonjwong This is a very nice answer, but incorrect format. See [this article](https://stackoverflow.blog/2011/07/01/its-ok-to-ask-and-answer-your-own-questions/) for more info on how to correctly put something like this on StackOverflow :) – Yksisarvinen Jul 11 '18 at 14:39
  • Hi, I'm new here, I just posted this info, since I thought it would be helpful. Thanks for sharing the article for the proper format! – claytonjwong Jul 11 '18 at 14:57
  • Looks like it might be too late to modify the original post. Next time I'll make a Jeopardy style question and answer. Thanks! – claytonjwong Jul 11 '18 at 15:00
  • @claytonjwong you should still be able to edit the post? Just click the edit button just above the comments – Alan Birtles Jul 11 '18 at 15:50
  • 1
    I've deleted the answer for you, you should be able to copy and paste the original post from the edit history into a new answer, click the "source" button to get to the original markup – Alan Birtles Jul 11 '18 at 15:53
  • Thanks @AlanBirtles! – claytonjwong Jul 11 '18 at 16:37

2 Answers2

34

Steps to use C++17 in Xcode (9.4.1) on OSX High Sierra (10.13.5):

  1. Open existing or create a new C++ project in Xcode
  2. Click on the "show project navigator" button. It is located on the top-left section of Xcode window just below the minimize/maximize/close window buttons. It is the left-most icon and looks like a folder.
  3. Click on "Build Settings" and scroll down to find and expand the section "Apple LLVM 9.0 - Language - C++"
  4. Change the C++ Language Dialect combobox selection to "C++17 [-std=c++17]"

Xcode Build Settings

Verification steps:

Now when I output __cplusplus, I see 201703, and I am able to compile C++17 features, such as 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;
}

Output:

201703
Program ended with exit code: 0
claytonjwong
  • 814
  • 1
  • 7
  • 13
  • 5
    FWIW, this setting was under `Apple Clang - Language - C++` instead of `Apple LLVM 9.0 - Language - C++` for me - OSX 10.15.6, Xcode version 11.7. – James Whiteley Sep 08 '20 at 20:53
5

When using development CocoaPods (writing a C++ library) I had also to update podspec of this library containing c++ 17 code to make compile host application which included this pod.

So I added these flags to library's podspec

  spec.xcconfig = { 
    "CLANG_CXX_LANGUAGE_STANDARD" => "c++17",
    "CLANG_CXX_LIBRARY" => "libc++"
  }
schmidt9
  • 4,436
  • 1
  • 26
  • 32