C++11 with Xcode on Mac OS X Snow Leopard - c++

I have project that is at times developed on Windows/Visual Studio 2010 and uses some C++11 features and I need to be able to develop/build on Mac OS X. When I tried to build the project with Xcode I got a lot of errors around new C++11 features and checked the gcc version to find it's pretty old (4.2).
It looks like Apple is trying to force developers to pay an unnecessary upgrade to Lion by refusing to allow Xcode 4+ to be downloaded on any other version of Mac OS thus I'm left with Xcode 3.x.
How can I continue to use C++11 on Snow Leopard? Is there a way I can do this and keep Xcode as an IDE?

Update feb.25 2012: There are now a lot of features available for you to work with using the latest clang. Maybe you could target 10.6 if you use language features only. If you need library features, you will need 10.7.
given the present (sept.24.2011) state of the Xcode toolset, it's easiest (IMO) to choose another ide or os if you need c++11 support.
the fork of gcc xcode uses will never support these features. clang is pretty far behind wrt c++11 features (because its c++ implementation is still very new and other compilers have had a few extra years). therefore, the compilers xcode ships with do not presently support enough features for c++11 development, regardless of the version of osx you use.
you can install a newer version of gcc and use another ide fairly easily.
technically, you can also make a plugin for xcode 3 (not officially supported) which invokes another compiler (e.g. a more recent release of gcc). that route has been closed in xc4 (afaik). Update apparently, it's still available in Xc4; see idljarn's comment below.
for many projects, it's easier to just use your mac to boot into linux or windows (or use virtualization).
your final option is intel's compiler, which can be used in xcode and provides a decent amount of c++11 support -- try it with xcode before you buy to see if it fits your needs, plays well with xcode, and supports the c++11 features your team uses.
lastly, i don't think they do this for your upgrade money. they really don't maintain xcode for multiple releases very well - they just expect you to stay with the latest and greatest unless you need backwards compatibility; you just stop upgrading in that case. they invested in and assisted development of clang after gcc's license changed. so yeah... osx has always been very far behind wrt c++11 support because they decided to switch to another (very young) compiler. if you wait for xcode to support c++11, you will have to wait for clang to support it, which can be quite some time.

I just saw this now and I would like to update you on this. LLVM currently shipping with XCode is at version 3 (Source). This current version is very good with supporting C++11. Here is a link to what is supported:
http://clang.llvm.org/cxx_status.html
You can compare this with the current GCC support here:
http://gcc.gnu.org/projects/cxx0x.html
As you can see, Clang is currently not far behind, if it is at all, with features of the new standard being supported. The only thing that I see concerning is the lack of support for concurrency features, but that is the case for most C++11 compilers due to the nature of supporting it.So I hope that this answer is not too late and that you are not deterred. Go get the latest version of Xcode and fire away (If you have not done so already ;) )!

Related

Does OS X 10.7 libc++ support c++14?

I'm updating a project to use the latest Qt and taking the opportunity to switch to libc++ and C++11 at the same time. My understanding is that OS X 10.7 and above support this without me needing to ship my own build of the standard library. I'm happy to no longer support OS X versions <10.7.
C++11 support is great, but I'd really like to use C++14.
Does the libc++ installed for end-users with OS X >10.7 also support C++14 standard library features?
My guess is 'Yes' and that it's kept up to date by OS updates in >10.7 - but I've been unable to find a definitive source stating this to be the case.
Probably not. Apple shipped the last update for Mac OS X 10.7 (10.7.5) in October 2012; it's unlikely that the libc++.dylib has been updated since then (the only exception that I could think of is for security issues)
Xcode ships with a set of headers that change with what SDK you target; if you target a particular system (say 10.7), you get those headers.
Compare the contents of /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/ and /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/ too see what I mean.
For template-only features, you could use your own set of headers from a later LLVM release - but then you would be taking the burden of ensuring compatibility upon yourself.

Binary compatibility (using C++11) on older distro versions

I am using GCC to compile a C++ application on Ubuntu 13. I want to be able to use C++11 features in my code, but at the same time still be able to produce a binary that my users can run on older versions of Ubuntu.
If I compile on Ubuntu 13 with the latest version of GCC my binary will not run on Ubuntu 12 since glibc is not forward compatible:
(How compatible are different versions of glibc?)
What are my options?
Is this even possible without requiring my users to jump through massive hoops?
If not, what do my users have to do to be able to run the binary (i.e. can they install the newer glibc on the older version of Ubuntu)?
Note: I don't not want to consider statically linking glibc since:
I've read that this is a very bad idea
Licensing issues
Cross-distribution compatibility issues
Currently my application does not use any C++11 features and I compile on an older version of Ubuntu with an older version of GCC to avoid this problem. But it makes me sad not being able to use the latest and greatest language features :(
You can try to use Boost Libraries which have quite the same features as C++11 and is "more retro-compatible" than C++11 : it will easily compile on older version of Ubuntu.
Otherwise the best option might be to ask to the users of Ubuntu 12.04 to upgrade there GCC from 4.6 to 4.7 or more recent :
http://www.swiftsoftwaregroup.com/upgrade-gcc-4-7-ubuntu-12-04/
You are asking "how do I use code that isn't on older systems".
The answer is of course, "Include the code with your project".
If you think through what you're asking, you'll realize that in any case, you'll need the code for the c++11 functions in libstdc++. So if they aren't on ubuntu 12, YOU have to add them. Therefore, you'd have to have it statically linked. it's the only way to ensure it will run on an arbitrary ubuntu12 system.
Well you could make a fancy installation, but in the end, it'd just be your apps "dynamically linking" to the libstdc++, so it may as well be statically linked, since no other program is going to be looking for it on ubuntu12
In general, a c++ library is compatible only if the same compiler is used and (!) the versions of the compilers are matching (you might be lucky, though). There is no way to be portable in this sense, besides writing C-code.

C++11 compiler for windows

I was just watching a few videos on Channel9. I found things like lambdas really cool. when I tried to copy the example, it failed. auto didn't work either.
I'm using Nokia's qtcreator which ships with gcc-4.4.0.
I wanted to know which compiler has the fun features implemented so I could play around and learn. I'm not anti MS or anyhting so I don't mind trying Visual Studio if it has those features.
Nearly all C++11 features are implemented in the GCC 4.6.x . A good place to get latest GCC builds (MinGW) is the TDM website - http://tdm-gcc.tdragon.net/download . It should be easy to replace your current GCC with TDM GCC by overwriting files...
A special version of MinGW:
MinGW-Builds gives you everything gcc offers (currently 4.7.2)
That is: Including support for std::thread, std::async, std::future and friends.
As far as I know that's by far the most complete C++11 you can get on Windows.
You just get the MinGW-build binaries here. Unlike other gcc-based installations it supports posix threads, which are currently key to getting the gcc support for C++11 threads and friends working on Windows.
Extract the directory mingw to any location and add the following two paths to your PATH environment variable: (well, change F:\coding ...)
F:\coding\MinGW\bin
F:\coding\MinGW\msys\1.0\bin
Add both, separated by semi colon. You will need to log out or reboot. To verify that you got it right, you can open a command prompt, and write
g++ --version
You should get a response like this, mentioning MinGW-builds:
g++ (Built by MinGW-builds project) 4.7.2
Copyright (C) 2012 Free Software Foundation, Inc. ...
I wrote a more complete instruction for getting this going with Eclipse, here: http://scrupulousabstractions.tumblr.com/post/36441490955/eclipse-mingw-builds
For playing around and learning C++11 on Windows, I recommend MinGW Distro by Stephan T. Lavavej. The newest version contains GCC 4.8.2 and Boost 1.55.0.
Take a look at MinGW-w64 if you're looking for a gcc-compatible compiler on Windows that supports a number of C++11 features.
Also if you're just looking for lambas and auto, as well as some other C++11 features like decltype, etc., (again, not as many feature are implemented compared to the latest stable gcc branch) then you can also use the free Visual Studio 2010 Express for C++ on Windows.
See here for an overview of the compilers and the supported C++11 features.
Scott Meyers maintains a webpage here:
C++11FeatureAvailability
The First link on the Webpage is:
Apache Wiki Overview of C++11 Support in Several Compilers is what you should have a look at.It doccuments in detail C++11 features supported by all popular compilers.
The Visual Studio 11 preview also supports lambdas.
Visual Studio 2017 has support in C++11, C++14, & C++17. + some of the Modern C++ "experimental" modules. If you decide on Visual Studio, you set the C++ standard at the project properties. C++11 is by default, but you can set it to C++14, C++17, or latest draft.
As regards g++, C++0x feature support should be detailed here: C++0x/C++11 Support in GCC - GNU Project - Free Software Foundation (FSF)
If you want to test most of the C++11 syntax using a Windows machine you have two options:
Install Cygwin and compile from sources gcc-4.7 (latest snapshot) and clang++ with libstdc++. However it is not guaranteed that this will work.
Safest bet: Install a modern Linux (such as Ubuntu if you are a Linux newbie) in a virtual machine (VirtualBox is a free virtual machine application) and in this virtual machine compile gcc-4.7 and clang++. I was able to successfully compile both of them on Ubuntu 11.10 following the instructions from this website.
Best option, as of 2014, is to use Visual Studio 2013 updated with the latest CTP (this will work even for the Express edition).
Few Min-GW Compilers do not support C++ Version 11 or later. This version is required for thread in OpenCV. So I will suggest using TDM-GCC Compiler instead of MinGW Compiler. Install this compiler and set path C:\TDM-GCC-64\bin to the system's environmental variable.

Can I use C++11 with Xcode?

I am considering the use of some C++11 features (like auto for instance) in some cross-platform projects (Windows+Mac). On Windows, Visual Studio supports parts of the upcoming C++11 standard that would allow me to simplify parts of the code base so naturally I would be interested in starting to use these features.
But as far as I am aware, the current XCode version (3.2.4 + GCC 4.2) does not support any C++11 features at all. Can I upgrade the GCC version or the CLang version somehow? Or should I just bite my tongue and wait for Apple to package a new version sometime in the future?
Xcode 4.2 had finally added support for C++0X:
In the project build settings screen, switch on "All" options.
In the "Build Options" section, set compiler to "Apple LLVM compiler 3.0".
Scroll down to "Apple LLVM Compiler 3.0 - Language" section and set "C++ Language Dialect" to "C++0X" and "C++ Standard Library" to "libc++".
The std::move(), move constructor and R-Value reference are known to work as expected, and I'm testing on the std::thread and std::atomic.
======= Update 2012: =======
Start with Clang - Many C++11 features are now available in Clang. It's included with Xcode.
======= Original answer from Jan 2011: =======
intel's compiler may be the cleanest way to go at this time.
http://software.intel.com/en-us/articles/intel-composer-xe/
clang's promising, but not particularly stable or featured wrt c++0x features. c++ is still very new for clang.
gcc: relatively mature, but you'll have to write and maintain your compiler plugins for xcode.
you can also specify custom scripts, but that is a pain to maintain... unless you go all out and create an adaptor tool.
Xcode uses the GCC or the Clang C++ compilers. Any features supported by those compilers are fair game. GCC's C++ compatibility page is here and the Clang C++ compatibility page is here.
I've found auto, decltype(), range based "for (:)" work in a cross platform project (LLVM for MacOSX,iOS, gcc/linux, MSVC 10/windows).
lambdas & variadic macros don't appear to work under LLVM yet sadly.

Can I upgrade Xcode to support a newer version of GCC to learn C++0x?

I would like to jump in learn C++0x, which has matured to a level I'm happy with.
Xcode on Snow Leopard 10.6 is currently at GCC 4.2.1, and the new features I'd like to try, like std::shared_ptr, lambdas, auto, null pointer constant, unicode string literals, and other bits and pieces, require at least 4.3 (I believe).
Ideally I'd use Xcode but I'm not even sure if you can manually upgrade the compiler for Xcode. Is this possible?
Otherwise, what is the best way to install a different version of GCC that doesn't interfere with the rest of the system?
I ended up downloading the latest Intel Compiler for Mac trial, and it does what I need. It's a good way to test the waters without messing with your system.
http://software.intel.com/en-us/intel-compilers/
You may want to look at LLVM and Clang and see if those can accomplish what you're after, if you want to use XCode. Apple has been making a shift away from GCC and towards LLVM/Clang due to licensing reasons.