Clang, LLVM, and g++ - c++

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?

Related

To exclude /usr/include/c++/4.3/ in compiling code with intel compiler

I am working on a cluster which has older version of intel compiler (11) and gcc (4.3).
I have installed a newer trial version of intel composer xe (with 14.0 compiler). I have also installed gcc 4.9. Both the newer gcc and intel compilers are in my home directory (non-root)
I use C++11 in my codes, so obviously i use -std=c++11 flag for compiling. I provide -L and -I flags to include intel's includes and libraries in my makefile
When I try to compile my code with icpc, The compiler looks in /user/include/c++/4.3/.... path.
I tried to remove the path by setting C_INCLUDE_PATH and CPLUS_INCLUDE_PATH to
/home/peter/intel/composerxe/include.
But it still looks in the /usr/include/c++ path. Because of this, the old files in /user/include/c++/4.3/tr1_impl/ ... are included and not the latest ones.
How do I stop the intel compiler to look into these paths and see the new ones. Now instead of intel compiler i use gcc4.9, what changes do i need to make ?
I tried adding -nostdinc flag for compiling, but no luck. It gives error as :
catastrophic error cannot open source file "iostream"
because 1st include header is iostream
I figured out that intel shares its headers from already installed gcc in the system and takes the /usr/include or usr/local/include paths.
In my case, the operating system on our cluster is SUSE linux with gcc4.3 as default GNU compiler.
Now, the cluster management software is set such that two compilers (intel and gcc) cannot be loaded simultaneously (module intel version 11 and module gcc version 4.5)
After I did a gcc --print-search-dirs I found out that the intel compiler inherits some common headers (iostream, etc) from existing gcc compiler (which is 4.3 in this case) and hence includes the old tr1_imple files and headers because of which I get errors in compiling c++11 code.
I did install gcc 4.9 in my home directory, but intel 14 has compatibility issues in including headers from gcc 4.9 and again this time weird compiler errors.
While using gcc 4.5 (using module load gcc-4.5) atleast got me to compile the code, I think the only solution to this problem would be to use gcc 4.7.2 and provide its include path since that was the first release with c++11 support ( not c++0x working draft)implementing a lot of c++11 features except concurrency.
One needs to know that Intel compilers do depend upon existing gcc compiler provided by the OS or installed by the user/root. Without that, they are pretty much of no use.
Cheers!

Is g++ compiler the same with the gcc compiler for C++? (Mac OS X)

I just need to make clear one thing. In University we are learning the C++ programming language and they suggest us to use the GNU C++ Compiler which is part of the GCC.
So on my Mac OS X Mavericks I download the command line tools from the developers.apple.com.
I wrote a simple C++ program and I compile this program using the g++ command like this:
g++ program.cpp
./a.out
And the program runs perfect. But as I know, using a different compiler, means that you have to use the correct syntax/commands/libraries for this spesific compiler, so while in the University we use the "GNU C++ compiler", I just want to make clear that with the g++ command is meant that I use the "GNU C++ Compiler".
Cheers.
Traditionally, gcc and g++ are both components of the GNU C compiler suite. gcc is the C compiler, and g++ is the C++ compiler.
On current versions of Mac OS X, the commands gcc and g++ are both treated as alternate names for clang and clang++, which are components of the Clang C compiler. However, this compiler is almost entirely compatible with GCC — the few differences that do exist will almost certainly not come up in the coursework you're doing.
(The most significant difference is that Clang's diagnostics are much better: it will point out exactly where a syntax error occurs in a line, rather than just what line it's on, and it can often identify potential typos or subtle mistakes in situations where GCC would just give you a cryptic error message. If you're just learning C, you will appreciate this a lot.)
As per #duskwuff, I would prefer to use clang++, however if you must be compatible, then you can installi the real GNU compiler via macports.
After installing macports (which includes a xcode-select step), simply do:
$ sudo port selfupdate
$ sudo port install gcc46
(or gcc47, etc.)
The compiler will be in your $PATH (if you set-up macports correctly), but explicitly, it will be /opt/local/bin/gcc46 (see sudo port select gcc).

ICC (14.0) cannot even compile hello-world in Ubuntu 13.10?

New to Linux, I just installed a Ubuntu 13.10 64 bit, with Eclipse 3.8 and intel's ICC 14.0.
The GCC come with Ubuntu is version 4.8.1.
So far ICC installed cannot even compile hello-world in Eclipse due to it cannot compile iostream header.
The iostream header will include another header, called c++config.h, stored in /usr/include/x86_64-linux-gnu/c++/4.8/bits
The iostream header itself stored in /usr/include/c++/4.8
The GCC compiler can compile the hello-world cpp and other c++ files perfectly, yet the ICC cannot even compile hello-world, tell me it cannot find c++config.h header, even if I include its path in the project.
Anyone know any work-around for this?
You can:
Add system include on command line
icc -idirafter /usr/include/x86_64-linux-gnu/c++/4.8/ -I... -L... my.cpp
Permanently add it to icc.cfg
cat /opt/intel/composer_xe_2013_sp1.1.106/bin/intel64/icc.cfg
-idirafter /usr/include/x86_64-linux-gnu/c++/4.8/
I successfully used this to compile scipy.
Two possibilities exist without changing too much of your setup:
Install an older version of GCC alongside the default one, assuming that ICC will work with some version, just maybe not the very new one you have now.
Install a newer version of ICC. It looks like you may already have the latest, but perhaps there is a patch release. I somewhat doubt it though.
This kind of thing happens fairly often when mixing and matching different compilers and standard libraries. It helps if both are of roughly the same age.

Compile C++11 code on mac?

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.

Using homebrew, gcc and llvm with C++ 11

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.