User Name
Password
AppleNova Forums » Programmer's Nook »

C++11 in XCode?


Register Members List Calendar Search FAQ Posting Guidelines
C++11 in XCode?
Thread Tools
ezkcdude
Veteran Member
 
Join Date: Jan 2005
 
2012-04-02, 19:15

I'm trying to re-learn C++ (been several years), and the book I picked up (C++ Primer Plus) has a lot of examples using features or libraries in C++11. One example is the <array> template. Anyhoo, here is an example of code that fails:

[PHP]// choices.cpp -- array variations
#include <iostream>
#include <vector> // STL C++98
#include <array> // C++0x
int main()
{
using namespace std;
// C, original C++
double a1[4] = {1.2, 2.4, 3.6, 4.8};
// C++98 STL
vector<double> a2(4); // create vector with 4 elements
// no simple way to initialize in C98
a2[0] = 1.0/3.0;
a2[1] = 1.0/5.0;
a2[2] = 1.0/7.0;
a2[3] = 1.0/9.0;
// C++0x -- create and initialize array object
array<double, 4> a3 = {3.14, 2.72, 1.62, 1.41};
array<double, 4> a4;
a4 = a3; // valid for array objects of same size
// use array notation
cout << "a1[2]: " << a1[2] << " at " << &a1[2] << endl;
cout << "a2[2]: " << a2[2] << " at " << &a2[2] << endl;
cout << "a3[2]: " << a3[2] << " at " << &a3[2] << endl;
cout << "a4[2]: " << a4[2] << " at " << &a4[2] << endl;
// misdeed
a1[-2] = 20.2;
cout << "a1[-2]: " << a1[-2] <<" at " << &a1[-2] << endl;
cout << "a3[2]: " << a3[2] << " at " << &a3[2] << endl;
cout << "a4[2]: " << a4[2] << " at " << &a4[2] << endl;
// cin.get();
return 0;
}[/PHP]

Anyone here know how to get this working in XCode or is it hopeless? If the latter, what should I do?
  quote
Majost
monkey with a tiny cymbal
 
Join Date: Nov 2004
Location: Lost
 
2012-04-03, 11:17

Unfortunately neither the included GCC (version 4.2) nor the included clang (version 3.0) in Lion have the necessary support needed for C++11.

In newer versions, it's as simple as adding the switch -std=c++0x or -std=c++11. Unfortunately GCC 4.2 doesn't even include such a switch. And while clang 3 has preliminary support for c++0x, it doesn't have the array from the new STL. So I'm afraid you're out of luck.

EDIT: I was missing the flags to use and link against the standard library. It works! If compiling on the command line, use the following command:

Code:
clang -std=c++0x -stdlib=libc++ -Xlinker -lc++ choices.cpp -o choices
In Xcode, you'll need to add all these options in the proper places.

Last edited by Majost : 2012-04-03 at 11:28.
  quote
ezkcdude
Veteran Member
 
Join Date: Jan 2005
 
2012-04-03, 13:00

Quote:
Originally Posted by Majost View Post
Code:
clang -std=c++0x -stdlib=libc++ -Xlinker -lc++ choices.cpp -o choices
In Xcode, you'll need to add all these options in the proper places.
Thanks! I'll try to find the options for putting that command in.
  quote
Posting Rules Navigation
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Post Reply

Forum Jump
Thread Tools
Similar Threads
Thread Thread Starter Forum Replies Last Post
XCode 3.0 feedback... scratt Programmer's Nook 8 2007-11-14 08:14
New Imac and Xcode Krientle Programmer's Nook 2 2006-12-20 14:12
XCode 2.3 is out... scratt Programmer's Nook 0 2006-05-24 03:35
Python in Xcode Oskar Programmer's Nook 7 2006-02-04 16:23
Xcode 2.2 is out... scratt Programmer's Nook 8 2005-11-18 06:14


« Previous Thread | Next Thread »

All times are GMT -5. The time now is 02:44.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004 - 2024, AppleNova