Use clang 3.0 + llvm 3.0 + libc ++ in Linux

Source: Internet
Author: User
What is clang/llvm/libc ++

Let's talk about the background to prevent some students from having no idea about clang/llvm/libc ++:

Clang is a C family language (including C/C ++/obj-C ++) that has developed very well in recent years (with the support of apple, a wealthy Apple) the front-end of the compiler, the so-called front-end, is that it can understand C/C ++/obj-C/objc ++CodeAnd convert it into a form that is closer to machine commands. Theoretically, "some form" is a syntax tree, but as a tool, clang actually helps you call the linker to generate executable code, which is the same as GCC.

Llvm is a general compilation optimization and code generation platform, which defines an intermediate language llvm IR, as long as the front end compiles the code into llvm IR, you can use llvm's rich optimization modules and code generation modules. In other words, llvm makes it easier to create a computer language, which has a profound impact.

Libc ++ is a C ++ Standard Library specially rewritten for clang. It is also considered as the "royal" library of clang. This is like the relationship between libstdc ++ and GCC, but clang can also use libstdc ++, which is one of the topics in this article.

Use pre-compiled clang

The newly released clang/llvm 3.0 can be downloaded here. It provides pre-compiled binary files of Debian/freebsd9/MacOS X/Ubuntu 11.04 and 11.10. If you use Ubuntu with enough versions like me, you just need to download the corresponding file, expand it, and copy the file to/usr/local. For example, if you download clangw.llvm-3.0-i386-linux-ubuntu-11_10.tar.gz

Tar xvf clang+llvm-3.0-i386-linux-ubuntu-11_10.tar.gz
CD clang + llvm-3.0-i386-linux-Ubuntu-11_10
Sudo CP-R */usr/local

In fact, this can be used. Even without libcxx, clang can use libstdc ++. For example, a classic hello. C (the content is unnecessary ),

Clang hello. c
./A. Out

You can see the results.

Add libc ++

If you want to use libc ++, you need to take down its code and compile it by yourself, but it is very easy. First, ensure that you have Subversion:

Sudo apt-Get install Subversion

Then download the code from the libc ++ code repository:

SVN Co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx

Then enter the libcxx directory and compile

CD libcxx/lib
./Buildit

If compilation is okay, A libc ++. so.1.0 file will be generated under the lib directory, copied to/usr/lib, and added with two symbolic links:

Sudo CP./libc ++. so.1.0/usr/lib
CD/usr/lib
Sudo ln-SF libc ++. So libc ++. so.1.0
Sudo ln-SF libc ++. so.1 libc ++. so.1.0

Then, since clang thinks that the libc ++ header file is located under/usr/include/C ++/V1, just link the libcxx/include directory to this directory. In other words, if you

Ls/usr/include/C ++/V1

It's right to see the familiar C ++ standard library header files.

Use clang ++/libc ++

Clang 3.0 supports several C ++ 11 features that are not supported by GCC 4.6. For example, to initialize non-static class members, check the following code (new_feature.cpp ):

 1 # Include <iostream>
2 # Include < String >
3
4 Class Myclass
5 {
6 Public :
7 STD :: String S = " Hello, world \ n " ; // Non-static data member initializer
8 };
9
10 Int Main ()
11 {
12 STD: cout <myclass (). S;
13 }

Row 3 initializes a STD: String member directly in the class declaration. This is something that can only be done in Java/C #. Now it finally enters C ++.Program. Compile command:

Clang ++ -- STD = C ++ 0x-stdlib = libc ++ new_feature.cpp
./A. Out

Output familiar

Hello, world

Our clang can be used officially.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.