What is the difference between Apple gcc and GNU gcc? Is Apple gcc a superset of the standard one?
The g++ version information in my OSX shows:
$ g++ --version
i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)
Some of the latest features of C++11 are in gcc 4.3/4.4 as per this. Is there any newer version of Apple gcc I can upgrade to. if so, how can i do it? I have Xcode 4.1
Well, for the first part, Apple in this case is using the LLVM backend for g++ as the default g++. Apple also installs the wonderfully named clang and clang++ front-ends for LLVM. However, there is absolutely nothing stopping you from installing newer branches of GCC; MacPorts has packages for everything up to 4.6. If you look for "APPLE ONLY" in the gcc man page, you can see what won't be available outside of Apple branches.
Beside the already mentioned llvm-gcc and clang, there is also an Apple-supplied gcc-4.2 (without LLVM backend) at /usr/bin/gcc-4.2 in Xcode 4.1. But do not overwrite the Apple-supplied versions in /usr/bin. All three support a superset of features include multi-arch support and multi-abi support not found in the vanilla GNU distributions and many third-party packages depend on these features in OS X. If you install something via MacPorts or from source, it will be installed to a different path, like /opt/local/bin or /usr/local/bin. Use PATHs or environment variables to manage which compiler you use.
You can use macport to install newer versions. You can download it here. Once you have installed gcc with macport, you can use it with xcode by adding an user-defined setting to your build :
- Go to the build setting of your project
- Click on the add build setting button
- Choose user-defined setting
- Name it CC
- In the value field, put the path of the gcc version installed by macport.
One thing that definitely is present in the Apple GCC branch but not in GNU GCC is blocks.
Related
Note: None of the answers provided at Get Apple clang version and corresponding upstream LLVM version seems to work anymore.
The download page at http://releases.llvm.org/download.html and the Wikipedia article at https://en.wikipedia.org/wiki/Clang seems to indicate that the most recent Clang version is 6.0.0.
But on my macOS High Sierra version 10.13.3, I see this output:
$ clang --version
Apple LLVM version 9.1.0 (clang-902.0.39.1)
Target: x86_64-apple-darwin17.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
$ which clang
/usr/bin/clang
This does not seem right. Why is the Apple's version of Clang greater than the current version of Clang?
How do I find out which version of Clang hosted on http://releases.llvm.org/download.html does my Mac's version of Clang correspond to?
I ask this because I see http://releases.llvm.org/6.0.0/tools/clang/docs/UsersManual.html documenting a -pedantic option for the clang command line, but it is not documented in the man page of clang on my system.
$ man clang | grep pedantic
$ clang --help | grep pedantic
$
launch terminal and enter:
clang --version
Apple clang version 11.0.0 (clang-1100.0.33.16)
This wikipedia table maps Xcode and Apple clang versions to LLVM versions.
Your clang-902.0.39.1 appears to be based on LLVM 5.0.2.
You could try using
echo | clang -dM -E - | grep __clang
and maybe that will give you more reliable numbers. Although I'm speculating since I'm not on a Mac.
Also, clang has had the -pedantic option for a long long time - many versions before 6.0. -pedantic is a GCC option and clang takes after GCC in its options.
The original question asked specifically about determining the corresponding "llvm.org" Clang version for a give Apple Clang compiler, which doesn't seem to be well-addressed by the other answers. In early versions of Apple Clang, the actual base LLVM version was included in the output of the clang --version command. That hasn't been the case for years now, and many sites that list the LLVM versions don't include info past the point where Apple stopped providing it.
Apple maintains a public fork of the "llvm-project" source code on GitHub. As far as I know, this is the only reliable way to determine the base LLVM version for a given version of Xcode/Clang/Swift. Apple overrides the LLVM and Clang version numbers as part of the build process, but the original LLVM version is defined in the CMakeLists.txt file for the llvm subproject. You can use the following steps to find the version for your current Xcode:
Run swift -frontend -version to get the Swift version for the installed Xcode. This seems counter-intuitive if you are trying to find the version for Clang, but Apple tags their open-source repo based on the Swift version.
Open https://github.com/apple/llvm-project/blob/next/llvm/CMakeLists.txt in your web browser. This is the file that contains the LLVM version numbers, but you will first land on the current development version and not the actual version that you want.
In the top left corner next to the filename, look for the drop-down button with the word "next". If you are familiar with GitHub, you know that this is the branch/tag selector.
Click on the drop-down, switch to the "Tags" view, then search for swift-<version>-RELEASE, where <version> is the Swift version number from step 1. For example, Xcode 13.4 uses Swift 5.6.1 so you would search for swift-5.6.1-RELEASE. Click on the tag name to select that revision.
In the CMakeLists.txt file, look for the line that sets the LLVM_VERSION_MAJOR variable. The MAJOR, MINOR and PATCH versions will give you the exact version of LLVM on which the Apple build is based. For Xcode 13.4, where Apple Clang reports a version of 13.1.6, you will see that it is based on LLVM 13.0.0.
The major version for Apple Clang is always the same as the major version of the corresponding Xcode. The fact that the major version of Apple Clang and the base LLVM happen to match in the Xcode 13.4 example is purely coincidental. Clang 13.1.6, which was first released with Xcode 13.3, is the first release in years where this has been the case, although the minor and patch versions are obviously still different.
Also be aware that Apple Clang is only based on the corresponding "llvm.org" source code. It is built from a fork that may include some Apple-specific differences, and there are also additional code changes in the proprietary Xcode release of Apple Clang that may not appear in the Apple open-source fork on GitHub.
Finally, many default values can be specified at build time and may be different between the Apple and "llvm.org" Clang compilers. For example, Apple Clang sometimes uses different default C and C++ standards when a specific standard version is not specified on the command-line. Apple Clang 13.1.6 and "llvm.org" both default to C17 for the C standard, but Apple still defaults to the original C++98 standard for C++ while "llvm.org" Clang 13 defaults to C++14.
I am an expert on this.
If you use "clang --version" to checkout the version of the clang compiler on your mac, then as you got that:
Apple LLVM version 9.1.0 (clang-902.0.39.1)
Target: x86_64-apple-darwin17.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
What you want to know that which the actual Clang version on Mac?
In other words, you want to ask, which the actual Clang version for your Xcode is been used on Mac?
As your Mac terminal output,(The output tells you things about your Xcode on your mac)
the first line "Apple LLVM version 9.1.0 (clang-902.0.39.1)" means:
your installed Xcode version was 9.3 or 9.3.1, including the default installed Clang version 9.1.0(This Clang version Identifier wasclang-902.0.39.1).
the second and third line do no matter with your Clang Version.
the fourth line "InstalledDir: /Library/Developer/CommandLineTools/usr/bin"means:
Where is your now using Clang locations. Or, if you want to know which clang version are your Xcode(mac) using? you need to go that directory /Library/Developer/CommandLineTools/usr/bin. Just use this command to checkout which clang version your Mac(==Xcode) are using now:
cd /Library/Developer/CommandLineTools/usr/lib/clang/ && ls
That may show like this: 9.0.0 or 9.1.0 or 10.0.0.
As this may show, The Clang version of your Xcode Now may use 9.0.0, or 9.0.1 or 10.0.0.
But why your now using Clang version is different from the default installed Clang version of Xcode including?
Or, why there is another Clang version out of the default including Clang version of Xcode?
Or, Xcode comes with Clang, why there is another version Clang?
As I know, the newable version of Xcodes are including the Command Line Tools. Or, the new version of Xcode comes with Command Line Tools; or, If you use Xcode, the Command Line Tools are also embedded within the Xcode IDE. And the Command Line Tool are including many useful tools, such as
the Apple LLVM compiler(LLVM-Clang), linker, and Make.
Also, Why you have a Command Line Tools of Xcode and another separated Command-Line Tools. Maybe you install a separated Command Line Tools after installing Xcode! And, you may select the separated Command-Line Tools via using "xcode-select --switch <path>" to replace the default Command Line Tools of Xcode
according to the old blog's guidance.
Just like this image(from Chinese Website juejing)
As I see, The Xcode.app was stored in /Applications/Xcode.app/Contents/Developer. The separated Command Line Tools was stored in /Library/Developer/CommandLineTools.
Just like this image(from Chinese Blog Website juejin):
So, when you use "clang --version" to check out the clang version of your Mac you are using, that shows:
Apple LLVM version 9.1.0 (clang-902.0.39.1)
Target: x86_64-apple-darwin17.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
So, if you are using the default Xcode of command-line Tools, what would that "clang --version" output? Here comes the using default Xcode of command-line Tools:
Apple LLVM version 9.1.0 (clang-902.0.39.1)
Target: x86_64-apple-darwin17.4.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
All in all, if you have more than one xcode installed on mac or more than one separated command-line tools, you will have more than one command-line tools; Just like the image(from Chinese Website Blog juejin) shows:
Others, the connection of Xcode and command-line tools just like this image(from Chinese Website juejin)
Also, you will have more than one clang to choose from using.
Last, what i want to say is, which clang version are your mac using, that depends the directory of command-line tools's chosed InstalledDir.
If your mac use the default embeded Xcode command-line tools,
then, that "Apple LLVM version 9.1.0 (clang-902.0.39.1)" shows your using Clang 9.1.0 with it's Identifier clang-902.0.39.1, your using Clang installed dirctory was in
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
If your mac use separated command-line tools, then, that "Apple LLVM version 9.1.0 (clang-902.0.39.1)" ONLY express your installed Xcode 9.3/9.3.1 with embedded command-line tools(Clang 9.1.0) , Your mac Now used command-line tools was from Xcode. And, that "InstalledDir: /Library/Developer/CommandLineTools/usr/bin" shows
where Now your Mac using command-line tools(Clang compiler) was in
or where Now your Mac using Clang compiler was in.
Last Last Last,
Your macOS High Sierra version 10.13.3 could install Xcode version up to Xcode 10.1, and Clang version up to Clang 10.0.0. As your "clang --version" shows, you are not using the embedded Xcode command-line tools(Clang). Now You are using the separated (command-line tools)/Clang, enter Your InstalledDir show directory "/Library/Developer/CommandLineTools/usr/bin" to checkout Now Your Mac using version. Just using this command to checkout what the version of Clang you are using:
cd /Library/Developer/CommandLineTools/usr/lib/clang/ && ls
(Infer from the command-line tools downloadable on your computer's current system 10.13.3)
Take my macOS 10.12 for example, my Xcode could up to Xcode9.2, and Clang could up to Clang9.0.0, Because I have installed separated command-line tools with Clang 9.0.0, My VSCode C/C++ compiler just use Clang9.0.0 via the separated command-line tools,
My Xcode C/C++ compiler just uses Clang8.1.0 via the embedded Xcode command-line tools.
This has been plaguing me for awhile now. I am trying to compile a huge C++ file (I know it works as I it works fine on my Arch Linux computer at work). When I checked my GCC version on my mac It returns the following
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.1.0
Thread model: posix
I have also installed the most recent GCC version using Homebrew with
brew install gcc49
My question now is how do I apply that newly installed GCC version to be the default version that the terminal uses?
I am also aware that when you use homebrew to isntall gcc it names it gcc-49 so that there is no confusion between packages.
I have no idea how to replace the 4.2.1 version that comes with XCode with the 4.9 version I have installed.
Thanks
Edit:
Switched to my mac to get the full return statement of gcc --version
Edit 2:
My end game here is to be able to navigate to the directory and be able to type
make
sudo make install
to install the daemon that has been made. Right now that returns tons of errors with random packages and the Standard Library
By default, homebrew places the executables (binaries) for the packages it installs into /usr/local/bin - which is a pretty sensible place for binaries installed by local users when you think about it - compared to /bin which houses standardisded binaries belonging to the core OS. So, your brew command should have installed gcc-4.9 into /usr/local/bin. The question is now how to use it... you have several options.
Option 1
If you just want to compile one or two things today and tomorrow, and then probably not use the compiler again, you may as well just invoke the gcc installed by homebrew with the full path like this:
/usr/local/bin/gcc-4.9 --version
Option 2
If you are going to be using gcc quite a lot, it gets a bit tiresome explicitly typing the full path every time, so you could put the following into your ~/.bash_profile
export PATH=/usr/local/bin:$PATH
and then start a new Terminal and it will know it needs to look in /usr/local/bin, so you will be able to get away with simply typing
gcc-4.9 --version
Option 3
If you just want to use gcc to invoke the compiler, without worrying about the actual version, you can do Option 2 above and additionally create a symbolic link like this
cd /usr/local/bin
ln -s gcc-4.9 gcc
That will allow you to run the homebrew-installed gcc by simply typing gcc at the command line, like this
gcc --version
Note:
If you later want to install, say gcc-4.13 or somesuch, you would do your brew install as before, then change the symbolic link like this:
cd /usr/local/bin
rm gcc # remove old link from gcc to gcc-4.9
ln -s gcc-4.13 gcc # make new link from gcc to gcc-4.13
Note that if you are actually using C++ rather than C, you will need to adapt the above for g++ in place of gcc.
simply updating the order of $PATH in ~/.bash_profile to the brew installed version 'export PATH=/usr/local/Cellar/gcc/5.1.0/bin:$PATH' was not enough to make the switch for me
changing the alias in your ~./bash_profile (alias gcc='gcc-5') works, but can be confusing i.e. which gcc will return the Clang version
what worked for me was to make a symbolic link in the brew gcc directory as well as update the path (point 1 above)
cd /usr/local/Cellar/gcc/5.1.0/bin/gcc
ln -s gcc-5 gcc
now which gcc returns the correct version 5.1.0
OS X does not come with GCC installed (4.2.1 or otherwise). Clang is the default system compiler and has been for some time. It is using the C++ headers from 4.2.1 when invoked as GCC. Have you tried compiling your code with Clang natively, instead of calling "gcc" (which calls Clang)? It has more modern headers and C++ support than the GCC emulation mode.
Download the gcc binaries untar and copy the bin, lib include share and libexec files to your /usr directory then type gcc --version this is what i expect you to see
gcc --version
gcc (GCC) 4.9.2 20141029 (prerelease)
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
I installed gcc version 4.9 with homebrew in my mac. But when I check the gcc version in terminal it is showing still the old one.
Aarons-MacBook-Air:bin Aaron$ gcc -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.1.0
Thread model: posix
Please help
homebrew installs gcc with version specific suffixes, so when you install gcc49, it gets called gcc-4.9 on the command line. This is intended to prevent interference with the gcc stub (which is actually clang) which is provided by the OSX command line development toolchain, as well as to allow you to install gcc-4.8, gcc-4.7, etc. alongside each other.
Generally defining the environment variables CC=gcc-4.9 and CXX=g++-4.9 should allow you to compile autoconf based packages, as well as standard makefile based projects using the gcc-4.9 compiler, rather than using the default cc/gcc.
I'm trying to build a project which is originally written for archlinux. The building process works fine on that archlinux but has a huge amount of errors when I tried to build it on my mac. The project is basically written in C++11 so it's very unstable. I think there has something to do with the compiler because the clang version in mac is 4.2(It actually shows 'Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)' when I type clang --version) and 3.3 for linux. So I want a different version of clang, perhaps 3.3. How Can I specify the version of my compiler when trying to build the project?
Some might argue this belongs on apple.stackexchange.com, but I'm not sure C++11 tags grab a lot of attention there:)
Xcode 5.0 is now available from the App Store. Install it, go to Preferences -> Downloads, and install the Command Line Tools. However...
First, you will need to go to Preferences -> Accounts and add your Apple ID. For some reason, the Command Line Tools download requires an account login. I don't know what the rationale for this is.
> clang -v
Apple LLVM version 5.0 (clang-500.2.75) (based on LLVM 3.3svn)
If you are willing to build from the command line, using make for example (which sounds likely given the project is from Linux), then install Macports and install whatever version of clang you would like:
$sudo port install clang-3.1
$sudo clang_select
Note that this version of clang shouldn't interfere with the Xcode Command Line package if you are will to invoke the Xcode version using xcrun clang instead of plain old clang.
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.