LLVM學習之路(一) LLVM+Clang 編譯與安裝
================================================================================
原始碼下載
================================================================================
1.通過svn下載(需要安裝svn[可用命令:sudo apt-get install subversion ])
下載LLVM原始碼(建議重新命名為llvm):下載 Clang原始碼(建議重新命名為clang):cd where-you-want-llvm-to-live svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm
cd where-you-want-llvm-to-live
cd llvm/tools
svn co http://llvm.org/svn/llvm-project/cfe/trunk clang
下載 Clang-extra-Tools原始碼(建議重新命名為extra):cd where-you-want-llvm-to-live
cd llvm/tools/clang/tools
svn co http://llvm.org/svn/llvm-project/clang-tools-extra/trunk extra
下載LLD linker [可選,連結器]:下載 Polly Loop Optimizer [可選,迴圈和資料優化器]:cd where-you-want-llvm-to-live cd llvm/tools svn co http://llvm.org/svn/llvm-project/lld/trunk lld
cd where-you-want-llvm-to-live
cd llvm/tools
svn co http://llvm.org/svn/llvm-project/polly/trunk polly
下載 Compiler-RT(建議重新命名為Compiler-RT):cd where-you-want-llvm-to-live
cd llvm/projects
svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
下載Libomp [可選,如果需要openmp支援的話再下載]cd where-you-want-llvm-to-live
cd llvm/projects
svn co http://llvm.org/svn/llvm-project/openmp/trunk openmp
下載libcxx and libcxxabi [可選]:cd where-you-want-llvm-to-live
cd llvm/projects
svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx
svn co http://llvm.org/svn/llvm-project/libcxxabi/trunk libcxxabi
下載Test Suite Source Code [可選,llvm自帶測試,較大]cd where-you-want-llvm-to-live
cd llvm/projects
svn co http://llvm.org/svn/llvm-project/test-suite/trunk test-suite
下載所需要版本的對應原始碼即可
================================================================================
編譯安裝
================================================================================
可在llvm解壓同級目錄下新建llvm-build資料夾進入llvm-uild目錄:cd llvm-build
編譯llvm原始碼(儘量選擇Release安裝,比較快,預設Debug安裝會比較慢):cmake -G "Unix Makefiles" -DLLVM_ENABLE_ASSERTIONS=On -DCMAKE_BUILD_TYPE=Release ../llvm
(啟用多個執行緒編譯[這裡用4個])
make -j4
make install
至此,LLVM+Clang基本安裝完畢。
================================================================================
測試舉例
================================================================================
檢視clang版本:
clang --version
獲取幫助選項:
clang -help
也可以編寫簡單的helloworld程式hello.c來用clang進行編譯:
clang hello.c
./a.out
或:
clang hello.c -o hello
./hello
至於為什麼編譯安裝的LLVM+Clang卻只測試clang,那麼來看一下如何簡單的使用LLVM:
將hello.c編譯為LLVM IR的二進位制檔案(得到hello.bc):
clang -emit-llvm -c hello.c -o hello.bc
使用llvm虛擬機器執行hello.bc(輸出“hello world!”):
lli hello.bc
將hello.bc從二進位制轉換為可讀的IR檔案(得到hello.ll):
llvm-dis hello.bc
內容如下: