I'm new to C++11. I've tried:
clang++ -std=c++11 -stdlib=libc++ *.cc
It works, but my questions is:
Is there anyway to set these flag as default for clang++?
How to update g++ 4.2 to a version that supports C++11?
Which way do you think it's the best to compile C++11 code on mac?
Thanks.
Brett has described how to install GCC it with MacPorts. Here’s how to do it with Homebrew, which styles itself (rightfully!) as a modern replacement of MacPorts:
brew tap homebrew/versions
brew install --enable-cxx gcc48
As far as I know the easiest way to install the most recent Clang is by downloading the compiled version linked in dsign’s answer – and, as Brett mentioned, since Apple uses clang internally it’s not advised to tinker with that installation – just put yours somewhere else.
Concerning what the “best” compiler is there are two things to consider in addition to what Brett has already said:
GCC is much older and more mature than Clang. Internal compiler errors do happen occasionally in Clang. That said, it’s maturing rapidly because it’s being pushed by several companies.
Clang is feature complete for C++11, GCC 4.8 is not. One very obvious example of this is the fact that GCC 4.8 still has no working <regex> implementation, which is a shame.
Outside of an IDE (e.g., in shell), I normally have the variable CXX set to: "clang -std=c++11 -stdlib=libc++" in .profile / .tcshrc / etc., since this is picked up by most configure scripts too. On the cmd line I might use: $CXX -c foo.cc
MacPorts gcc-4.8.1 works well: "[sudo] port install gcc48 [-universal]"
"[sudo] port select --set gcc gcc48" will make this the default gcc, g++, etc.
Don't attempt to update or modify the system tools, like the old gcc-4.2 / llvm hybrid that comes with Xcode.
I don't know what you mean by 'best' way in the 3rd part of your question, but with Apple's support (they employ the primary author of LLVM), and other projects like FreeBSD behind it, clang will only continue to improve. It's already much faster than gcc, has far better error messages / diagnostics (especially for C++ and templates), and a modular architecture. For OS X, it's the clear choice.
Answers:
I don't think so
You install another g++ version alongside 4.2, it is bad karma to remove the one that comes with the system. To install a new one, check this
Same goes for clang. You can download it here.
In general, I totally recommend that you get better g++ compilers, 4.2 is quite old and its code quality is not as good. And using c++ 11 is totally worth it.
Do it all in Xcode's build settings like most Apple developers. Xcode simplifies life in many ways.
Related
I installed clang to compare its use with g++ (gcc) for some C++ programs; as in, I compared compilation time, error feedback, etc,. for the same programs. I did this mainly for the official LLVM tutorial (implementing a compiler using LLVM).
At one point, I needed to install the LLVM libraries (v. 2.9.), while before I had only downloaded clang. Subsequent to this, compilation with clang++ doesn't seem to work for the following tutorial code; while g++ appears to work with LLVM directives (e.g., 'llvm-config --libs`, etc.). Concurrent to installation of the libraries, I had to also go from the old tutorial I was reading (llvm 2.2., suggesting clang++ syntax), to one for 3.0. (suggesting g++ syntax), as the library include locations had changed.
My questions to explain the changes after downloading the llvm libs:
(1) When I now type 'g++', am I still actually working with gcc, or has llvm/clang set itself as a new default mimicking the gcc syntax? If yes, how can I make sure that I actually use gcc, not clang, when I want to?
(2) If g++ still is gcc, any opinions/guesses why the official clang/llvm tutorial switches from suggesting the use of its own competing compiler back to gcc? (c., eg, here)
My System:
Ubuntu 12.04.
llvm 2.9.
gcc 4.6.3.
clang 3.0.
In order for typing g++ to execute a clang compiler, you'd probably be looking at either a symbolic link called g++ to clang, earlier in your $PATH than the path to the real g++, or else an alias named g++, along the lines of alias g++='clang++'.
You can check for the alias (although I imagine it's unlikely) by typing alias g++, which will let you know if you have any alias set up (and if so, what to)
In order to change the $PATH issue, if it occurs, you'd have to either remove the symbolic link (seems sensible, given if you wanted clang, you could just type clang++ instead of g++) or change the position of the symbolic link in the PATH variable, but since they're likely to exist in /usr/local/bin or something similar, that would render a symbolic link unlikely too!
Given this, probably g++ still calls the gcc g++ compiler, and someone was just a little careless when typing the tutorial - I see only the one mention of g++ on the page you linked, near the bottom?
Here's my problem: I want to use C++11 features provided by either gcc or clang. However, I have these requirements:
I'm using a mac
I'm dependent on a bunch of libraries provided by homebrew (and really don't want to compile them myself). Specifically OSG, which itself is dependent on a ton of other libraries. And boost, though I can always compile that myself.
Homebrew seems to only want to use gcc (please correct me if I'm wrong). I can't find any options to switch to LLVM instead. While I understand that this might be due to the fact that not all libraries are compatible with LLVM yet, this would still be a nice feature for those that are.
The version of gcc that comes pre-installed on a mac of gcc is 4.2. gcc 4.2 doesn't have the c++11 features required. I've installed 4.7 via homebrew, but searches for how to set homebrew to use it all say don't do it (gcc 4.2 on the mac is not the vanilla version, so the 4.7 version I got won't be able to compile some things).
My questions are: Does anyone have any suggestions or fixes they have implemented to get around this problem? Should I give up on Homebrew? Does anyone know if Homebrew has a plan to switch to LLVM in the future? Does anyone have any upgrade-plan for how to deal with these incompatibilities?
I don't see how homebrew can continue to depend on gcc 4.2 in the long run, but haven't found any real discussion on this matter.
The default GCC on Mac is not real GCC of GNU. It's LLVM-GCC in fact, which is a branch of GCC. Several years ago, LLVM-GCC was terminated, and replaced with DragonEgg, which is a GCC plugin to use LLVM as a GCC backend.
LLVM-GCC is just a compiler frontend, whose role is using GCC frontend to translate the source code into LLVM IR[Intro to LLVM 11.3]. Once IR generated, LLVM backend will use it to generate binary code. This step has nothing to do with GCC.
The above goal was fully achieved from 10.7, whose components were all compiled by clang, a frontend provided by LLVM.
But Apple still kept LLVM-GCC and GCC runtime libraries. I guess its purpose might be providing a opportunity to compile some code GCC ONLY.
Now let's answer your questions:
If you want to use C++11 features, use clang++ -stc=c++11 -stdlib=libc++ instead. And clang might have already supported all c++11 features.
If you want homebrew supporting LLVM, it has already supported, at least on backend.
If you want homebrew using clang as a compiler frontend, it depends on homebrew community schedule. For example, you can append --with-c++11 argument to use clang to compile boost.But you cannot use this argument when brew install autoconf. In fact, some components might not be compiled correctly by clang.
If you know it can be compiled by clang but homebrew hasn't supported yet, you have to hack the corresponding ruby script at $HOMEBREW_ROOT/Library/Formula directory. Fortunately, in most of cases, replacing ./configure blablabla with ./configure blablabla CXX=clang++ -stc=c++11 -stdlib=libc++ works well. And by the way, if your hack is successful, please make a pull request to homebrew.
So, try it and have a fun.
I have an OS X Mountain Lion environment and use C++11. In this answer I'll break your requirement for not compiling your own stuff.
I use Homebrew and, I must say, I advise you to give up on depending on it to provide you clang and libc++ and all its formulas built with them.
What I've done, and I like, is
clone llvm, clang and libc++ from repositories.
install to /opt/local and put /opt/local/bin at top on /etc/paths.
build my development stuff with my new clang.
let Homebrew for installing tools like git and things I'll not develop for, just use.
I've followed clang build instructions for installing it to /opt/local.
For libc++, one detail: after running the buildit script, I've symlinked the include directory to /opt/local/lib/c++/v1 (clang on /opt/local looks for this as default directory), and also symlinked the libs to /opt/local/lib/ (but look that binaries will not automatically link to libc++ on /opt/local/lib. You must use install_name_tool for that).
use
clang++ -std=c++11 -stdlib=libc++
you can also install latest gcc from homebrew-dups
brew install [flags] https://raw.github.com/Homebrew/homebrew-dupes/master/gcc.rb
For LLVM, brew install --HEAD llvm. To use clang or a brew-installed gcc, add --with-clang or --with-gcc=gcc-x.x where x.x is the version you want.
I am currently starting to work seriously with C++. I've heard about the new features of C++11 and I like them. So I wonder whether I should write my new project according to the new standard. My current toolchain (that comes with XCode, I guess) does not support features like the auto keyword for type inference.
> g++
i686-apple-darwin11-llvm-g++-4.2
So I am looking for an easy and safe way to get a C++11 toolchain to try it out. I cannot risk breaking my old toolchain.
I know where to get binaries of GCC 4.8 for Mountain Lion, but I don't know how to install all the files manually (and would rather have a package manager do this for me). This discussion explains how to install GCC via homebrew, but I am affraid that this will overwrite and break my existing toolchain.
Also, I do not know how to configure a new toolchain in Eclipse after installation so I can use it with Eclipse/CDT.
You can use the homebrew package manager for OSX: Link
Have a look at https://github.com/mxcl/homebrew/wiki/Custom-GCC-and-cross-compilers and more specifically at homebrew dupes which has duplicates (but more recent versions) for software provided by OS X.
For a reasonable C++11 experience you should look for gcc 4.6 or gcc 4.7. When you have installed a recent version of gcc, you can then use it in your Makefiles. Mind you have to compile with -std=c++0x (gcc-4.6) or -std=c++11 (gcc-4.7+).
You can also look here How to enable C++11/C++0x support in Eclipse CDT? if you get syntax errors and warnings for C++11 constructs in Eclipse CDT.
I am new to programming, so have never experienced a language update. With the release of C++11, I want to make use of all the new features such as lambda expressions and threads. I know I can do this with external libraries but using native language features would be more convenient.
I am using gcc 4.2.1 on Mac OS X Snowleopard in Xcode 3.2.6
What all do I need to do and update to start using C++11 features?
You can update to Xcode 4.1 (or whatever the most recent version you can get for Snow Leopard is) and get a new compiler with a few more C++11 features. There are some posts here on Stack Overflow about getting better support for C++11 in Xcode 4.1 on Snow Leopard.
But even the latest compiler available through Xcode does not support some C++11 features like lambdas. To get the best C++11 support you'll want to install a newer compiler, gcc 4.6 or 4.7, or Clang.
I frequently build the latest version of clang from source. It's not difficult to do if you're familiar with building other open source software. I use the git repos for clang and llvm, http://llvm.org/git/llvm.git and http://llvm.org/git/clang.git. You can also find instructions on their website for getting started: http://clang.llvm.org/get_started.html. Once you have the source for clang and llvm it's just ./configure && make && sudo make install. (you might want to run the tests before installing, since this is directly out of the repository. After make do make check in the llvm directory, and once that passes cd down to tools/clang and run make test. If everything is okay then sudo make install)
I don't remember if Snow Leopard included libc++ or not, so you may need to get that as well. http://libcxx.llvm.org/
Once everything is built and installed you can do:
clang++ -std=c++11 -stdlib=libc++ main.cpp && ./a.out
and you should have just about the best C++11 support around.
Recent patches in clang have really improved support for the last features you're likely to notice as a new C++ programmer. There are still a few bits and pieces left, but as of 3.1, and as far as I'm aware, clang has every C++11 feature that either gcc 4.7 or VC++11 has and more besides. libc++ also has the fewest gaps in terms of C++11 standard library features IME (though I think VC++'s standard library will also be pretty complete once they catch up on language features, e.g. char32_t and char16_t as native types so that the standard's mandated specializations for those types can be used).
Basically you only need to get a toolchain that has support for the new features. In macosx that would be either GCC or clang++. You might need to download/compile a particular version if needed (i.e. if the feature that you want to try is not in a prepacked compiler bundle but is available in the repository).
I downloaded and compiled the latest trunk of clang++ from subversion to do some testing, and installed g++ 4.6 with macports in Snow Leopard. I remember that I had some issues with the setup of the environment, but don't quite remember which of the compilers gave me problems or how I solved them. Google is your friend there :)
If you have homebrew installed, from this article, just two steps:
brew tap homebrew/dupes
brew install gcc --enable-cxx --enable-fortran --use-llvm
I just suffer a problem that the all the things works well on my ubuntu.
However, I want to get things work on my mac, bad thing happens.
it shows the following errors
cc1plus: error: unrecognized command line option "-std=c++0x"
I am total new to mac stuff, I got the xcode 4 installed.
I guess there must be c++0x, but I wonder how can i configure it with waf.
Thanks a lot!!
I’m guessing you’re using GCC supplied with Xcode. That’d be GCC 4.2.1, a rather old version that won’t be updated by Apple in the foreseeable future.
You have essentially two options:
Xcode ships Clang/LLVM besides GCC, so you could use Clang/LLVM instead. That -std=c++0x option is recognised by Clang/LLVM but C++0x is not as fully supported as in recent versions of GCC. The LLVM project keeps a page listing their current C++0x support status.
Use a more recent version of GCC. You can either compile it locally or install it via one the open source package managers available on Mac OS X: MacPorts, Fink, Homebrew. I don’t really know if and which versions of GCC they’re able to build, so check with them first.
As Xcode comes with Clang on mac you can get c++0x support if you configure waf to use Clang.
In your wscript add to configure:
def configure( conf ):
...
conf.env.CXXFLAGS = [ '-std=c++0x', '-stdlib=libc++' ]
conf.env.LINKFLAGS = [ '-std=c++0x', '-stdlib=libc++' ]
....
Then run waf as:
CXX=clang++ waf configure
CXX=clang++ waf build
On Mac you can't go wrong with clang. You'll have to build the compiler yourself (using e.g. gcc-4.2 that you already have). It has -std=c++0x. The support for it isn't complete, but it is growing all the time. On the Mac you might also look at libc++ for C++0x support (combined with clang).