GCC is the de-facto standard compiler on Linux systems today. However, there is an emerging competing compiler family, the LLVM-based Clang along with a separate standard C++ library implementation, libc++, that is claimed to be a full implementation of the C++11 standard as opposed to GCC's
experimental support of C++11. In addition, LLVM is the technology behind Apple's new Swift programming language that is gaining acceptance fast, for now primarily in the Apple development world. Besides, Clang is the default compiler on Mac OS X and on FreeBSD starting with version 10.
So, what if you have a Linux box and want to be able to take LLVM/Clang/libc++ out for a spin? This is doable, but there are a few obstacles to overcome:
PrerequisitesIt is possible to remove GCC and then use repo-provided Clang to build libc++/libc++abi, but we are going an easier route: start with a system that has GCC installed. We will get rid of it later, after we have libc++(abi).We also need CMake, a tool for creating customized installation scripts for setting up software on the system. It provides functionality similar to that of the ubiquitous Linux configure scripts. We can install CMake from a repository. However, on some systems LLVM or Clang will complain that the CMake version is too old. If you run into this, you can build CMake from source that can be downloaded from the CMake website. It is possible to build Clang and LLVM using traditional configure scripts, without CMake, but libc++(abi) have to be built using CMake. In addition it is assumed that you are comfortable compiling and linking C/C++ code, managing software installation on your Linux system, and have a good working knowledge of the Linux command line. Installing Clang from RepositoryThere are two alternative approaches: installing Clang from a repository or building it from source. In the first case libc++ has to be built separately from source.The www. website offers pre-compiled binaries of LLVM and Clang with libc++ for some Linux distributions, and it's the easiest way, but only if your distro is supported.
Installing Clang (and LLVM that it depends on) from a repository should be trivial. However, you will still need to build libc++(abi). Also, Clang version provided by the repository is likely to be older than the most recent available source. Another difference between using repository-provided Clang and one built from source is that the former doesn't seem to understand the Building libc++ and libc++abiNow that we have installed Clang from repository, we need to build libc++. To use this library on Linux, we need an additional library that provides a low-level support for it, called Application Binary Interface, or ABI. There are a couple of alternatives described on the libc++ website, but we are going to stick with the libc++abi library supplied by the LLVM project. We will refer to this libc++/libc++abi combo as libc++(abi).
To build libc++(abi) we have to download and unpack LLVM and libc++(abi) source. We are only building libc++(abi) from source, but to accomplish this we must also have LLVM source code available. As of this writing, the tarballs available at are:
Side Note: Using GCC with libc++At this point you might be able to use your new installation of libc++(abi) with GCC, if you prefer to use GCC, but want a different C++ library implementation, i.e. libc++. Here is an example, where junk.cpp is a test C++ program you can write for yourself:
[root@test1 ~]# g++ -I/usr/local/include/c++/v1 junk.cpp
However, this will result in a bunch of linker warnings. The problem is that GCC will try to link the code using libstdc++, which is incompatible with libc++ headers located in /usr/local/include/c++/v1. Solution:
[root@test1 ~]# g++ -I/usr/local/include/c++/v1 junk.cpp -lc++ -lc++abi
[root@test1 ~]# ldd a.out
linux-vdso.so.1 => (0x00007ffc06f78000)
libc++.so.1 => /lib64/libc++.so.1 (0x00007fe5948df000)
libc++abi.so.1 => /lib64/libc++abi.so.1 (0x00007fe594668000)
libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007fe594360000)
...
From ldd's output we see that the binary still shows a dependency on libstdc++. If you want to get rid of it:
[root@ip-172-31-17-25 ~]# g++ -I/usr/local/include/c++/v1 junk.cpp -nodefaultlibs -lc++ -lc++abi -lm -lc -lgcc_s -lgcc
[root@ip-172-31-17-25 ~]# ldd a.out
linux-vdso.so.1 => (0x00007fff69df5000)
libc++.so.1 => /usr/local/lib/libc++.so.1 (0x00007f2c98c98000)
libc++abi.so.1 => /usr/local/lib/libc++abi.so.1 (0x00007f2c98a67000)
libm.so.6 => /lib64/libm.so.6 (0x00007f2c98765000)
libc.so.6 => /lib64/libc.so.6 (0x00007f2c983a4000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f2c9818e000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f2c97f72000)
librt.so.1 => /lib64/librt.so.1 (0x00007f2c97d6a000)
/lib64/ld-linux-x86-64.so.2 (0x00007f2c98f50000)
[root@ip-172-31-17-25 ~]#
Please note that GCC works with libc++(abi) for some code, but may fail for other. For instance, if you try to build LLVM+Clang, it may not work. If you want to build LLVM+Clang that doesn't depend on libstdc++, you will need to bootstrap, i.e. build it using libstdc++-dependent Clang. Installing Clang from SourceIf we want to build LLVM, Clang, and libc++(abi) from source instead of using repository-provided Clang, then we also need to download Clang source (cfe-3.6.2.src.tar.xz as of this writing) and do the following before creating the llvm_build directory and doing cmake; this is assuming the aforementioned directory layout and the tarball downloaded to the home directory. Please make sure libc++(abi) and LLVM sources are also placed in llvm_src as described earlier.
[root@test1 ~]# cd llvm_src/tools
[root@test1 tools]# tar xf ~/cfe-3.6.2.src.tar.xz
[root@test1 tools]# mv cfe-3.6.2.src clang
Then do cmake as described earlier. Please be sure to use the release build type! Otherwise you will end up with a compiler that is an order of magnitude slower than one built in release mode and is about 5 times the size of the release version!
Once cmake has completed, do
RPM-based Systems: Before Removing GCCIf we want to get rid of GCC and use clang as our default compiler on the system, we may have to make some adjustments on some RPM-based systems.
Clang does not provide a linker, but relies on the system's linker, typically
On an RPM-based system libgcc_s.so, provided by the gcc package, is typically a symlink to libgcc_s.so.1 provided by the libgcc package. So, before we uninstall GCC, we may want to create
Now it's OK to remove the gcc package. BTW, if you are tempted to get rid of libgcc and libstdc++, which are related to GCC, it won't work: too much of the system depends on those packages, as can be seen by trying to remove them. Making Clang Use libc++To build a binary from a C++ source file, say junk.cpp, we need to do what we did earlier when using g++ with libc++(abi):clang++ -I/usr/local/include/c++/v1 junk.cpp -nodefaultlibs -lc++ -lc++abi -lm -lc -lgcc_s -lgcc
If we want to use clang/clang++ with a typical ./configure script, we would need to set some environment variables, e.g.:
|
|
来自: astrotycoon > 《待分类》