Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enable C++17 in Xcode for Mac OSX?

Tags:

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

like image 414
claytonjwong Avatar asked Jul 11 '18 14:07

claytonjwong


People also ask

How do I run C ++ 17 on Mac?

For the language standard argument, try -std=c++17 and -std=c++1z for different compilers, one of them should work.

Does Xcode support C++ 17?

To get C++17 support, Xcode 9.3+ is needed, requiring at least macOS 10.13 on the build machine.

Do you need Xcode to run C++ on Mac?

To run a program in C++ in Mac we have to install Xcode or command-line tools for Xcode. Ways: Hence, there are two options to run a C++ program on Mac. Download and install by using Command Line Tools and using any preferred IDE or Code Editor for writing C or C++ code.


2 Answers

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 
like image 175
claytonjwong Avatar answered Sep 20 '22 00:09

claytonjwong


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++"   } 
like image 30
schmidt9 Avatar answered Sep 20 '22 00:09

schmidt9