Ever since switching to std=c++11 mode (or gnu++11, which seems slightly more stable), CGAL has been exceptionally crash-prone for me on OSX (there might have been more changes made at the same time; it was very chaotic).
We are building with Clang. This very dated FAQ entry suggests that the standard OSX compiler used to have issues with CGAL.
The default compiler on Mac OS X is g++ 4.0 (at least on Tiger and
Leopard). It has some bugs that are unfortunately encountered by some
programs using CGAL when optimizing. We recommend that you use a more
recent version of g++, such as g++ 4.2, which you can get by upgrading
to the latest XCode, or using Fink.
Is this still relevant?
About to try rebuilding CGAL (and header including libraries) without optimizations, but I'd like to know what the official support is for Clang on OSX.
Update
It's looking like user error. A number of APPLE #ifdefs were added during the C++11 upgrade (for unclear reasons), and removing them seems to be fixing many of the crashes.
According to CGAL's website, CLANG is supported.
http://doc.cgal.org/latest/Manual/installation.html#seccompilers
Related
I do most of my coding using Qt in C++ and have noticed that I get x86 architecture not found errors when I enable support for C++11 in my Qt .pro files.
I have the latest Ver of Xcode and have developer tools installed. I realize that OSX uses clang but it still seems that I am still stuck with OSX only supporting earlier versions of gcc.
Is there a reason we can't get these updates from Xcode, and if not I guess I need to install these updates myself?
You can use clang with this FLAGS -std=c++11 -stdlib=libc++ to use C++11 features, another similar question can be found here
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.
I heavily use the c++0x/c++11 features in my project, particularly code blocks and shared pointers. When I upgraded my OS to 10.8 Mountain Lion (Edit: From 10.7), I was forced to upgrade Xcode. In upgrading Xcode, I lost the ability to compile my c++ project for deployment on 10.6 systems as I get the following error.
clang: error: invalid deployment target for -stdlib=libc++ (requires Mac OS X 10.7 or later)
It appears that Apple is trying to force people to upgrade by not allowing developers to support Snow Leopard. This makes me angry. Arrrggg!!!
What can I do?
EDIT: After several comments back and forth, it should be made clear that 10.6 does not ship with system libc++ libraries. As a result, simply being able to build a libc++ project for 10.6 deployment is not enough. You would also need to include libc++ binaries with your 10.6 distribution or statically link to them. So lets continue with the premise that I am already doing that.
UPDATE 1: This question was originally intended for use with Xcode 4.5.2 (the latest version at the time the question was asked). I have since upgraded to Xcode 4.6.3 and have updated the question and answer to reflect that.
UPDATE 2: I've since upgraded to Xcode 5.0.2. The technique listed in the selected answer below still works as expected.
UPDATE 3: I've since upgraded to Xcode 5.1. The technique listed in the answer below does not yet work for this version!
UPDATE 4: I've since upgraded to Xcode 6.0.1. The technique listed in the selected answer below appears to be working again.
UPDATE 5: I've since upgraded to Xcode 7.1.1. The technique listed in the selected answer below appears to be working again, with one important caveat. You must disable Bitcoding used for AppThinning since the opensource LLVM version doesn't support it (nor should it). So you will need to switch between the open source and Apple LLVM clang in order to compile for both 10.6 and tvOS/watchOS (since Bitcoding is required for those OSes).
Apple has decided to only officially support libc++ on 10.7 or higher. So the version of clang/llvm that ships with Xcode checks to see if the deployment target is set for 10.6 when using libc++, and prevents you from compiling. However, this flag is not included in the open source version of clang/llvm.
Take a look at this thread:
http://permalink.gmane.org/gmane.comp.compilers.clang.devel/17557
So, to compile a project that is using c++11 for 10.6 deployment, you need to give Xcode the open source version. Here is one way of doing it:
Download the open source version of clang from here (use LLVM 3.1 for Xcode 4.5.x; use LLVM 3.2 for Xcode 4.6.x; use LLVM 3.3 for Xcode 5.0.x; use LLVM 3.5.0 for XCode 6.0.1; use LLVM 3.7.0 for XCode 7.1.1):
http://llvm.org/releases/download.html
Make a backup of Xcode's default clang compiler and put it in a safe place (In case you screw up!)
This is located at:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
Replace the default clang compiler with the one you downloaded from [1]
chown the clang binary for root:wheel with sudo chown root:wheel clang from the bin directory listed in [2].
Startup Xcode and compile!
UPDATE #1: This technique does not currently work for Xcode 5.1 or newer, which relies on LLVM 3.4. When I get some more time, I will try and find a solution to post here. But if someone comes up with a solution before me, they should post it as an answer.
UPDATE #2: Unfortunately I can't remember if I ever found a solution for Xcode 5.1, however I can confirm that the technique does still work for Xcode 6.0.1. I haven't tested on versions newer than that, but it could still work.
UPDATE #3: This technique appears to still work with XCode 7.1.1 using LLVM 3.7.0. However, the open source LLVM clang does not support Bitcoding. So you will need to switch between the open source compiler and Apple's compiler in order to develop for both 10.6 and tvOS/watchOS (which require Bitcoding).
P.S.: The Mac OS X binaries for LLVM 3.4 and 3.5.0 are listed as "Clang for Darwin 10.9" at www.llvm.org/releases/download.html rather than as "Clang Binaries for Mac OS X" in previous versions.
While Xcode 4.5.x is the current default version on OS X 10.8, you can have other, older versions of Xcode, such as Xcode 3.2.6 for OS X 10.6, available on 10.8 as long as you have access to their installers. You will need to ensure you install each one to a unique directory. Also, one thing you can't or shouldn't do is to install the Command Line Tools component or installer package of older Xcodes onto your 10.8 system, i.e. not into /usr or /System/Library. You can use the xcodebuild, xcode-select, and xcrun command line tools to access non-default Xcode components. See their man pages for more info. Older versions of Xcode are available to registered users of developer.apple.com
UPDATE: Based on your subsequent comments, I believe I did miss the point of the question and also that you had answered your own question. I think what you are saying is that you upgraded from 10.7 to 10.8, not from 10.6 to 10.8 as I assumed. You also did not make clear in the original question that you were distributing your own version of Apple's libc++ and friends from 10.7 with your own app. Apple does not make it easy in Xcode to do something like that since it has long been Apple's policy to discourage static linking with libs or distributing duplicate libs (which in some cases could violate license terms). There are good reasons for that policy.
The bottom line is that libc++ is only shipped with OS X 10.7 or later systems. There never was Apple support for libc++ in 10.6, so it's misleading to say it was removed. If you want to supply an app that is deployable on 10.6 and later systems and depends on libc++, the safest approach is to build your own clang/llvm and libc++ targeted for OS X 10.6 and use that to build your project. There are various ways to do that, probably the easiest is to use the MacPorts versions and set the deployment target in MacPorts for 10.6. Or build it all from scratch yourself. But modifying the clang compiler within Xcode 4.5 is a bad idea. And copying Apple libraries to one's app is generally a bad idea.
If you have a solution that works for you, great. But I would not recommend it to others.
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