2015-05-14 24 views
2

我是Linux新手,並通過一些clang教程工作。但是,我發現即使是一個簡單的文件也很難編譯。所以,這裏是部分代碼:用以下命令如何用clang/LLVM庫編譯C++代碼?

#include <cstdio> 
#include <string> 
#include <iostream> 
#include <sstream> 
#include <map> 
#include <utility> 
#include "clang/AST/ASTConsumer.h" 
#include "clang/AST/RecursiveASTVisitor.h" 
#include "clang/Basic/Diagnostic.h" 
#include "clang/Basic/FileManager.h" 
#include "clang/Basic/SourceManager.h" 
#include "clang/Basic/TargetOptions.h" 
#include "clang/Basic/TargetInfo.h" 
#include "clang/Frontend/CompilerInstance.h" 
#include "clang/Lex/Preprocessor.h" 
#include "clang/Parse/ParseAST.h" 
#include "clang/Rewrite/Core/Rewriter.h" 
#include "clang/Rewrite/Frontend/Rewriters.h" 
#include "llvm/Support/Host.h" 
#include "llvm/Support/raw_ostream.h" 

using namespace clang; 
using namespace std; 

當我試圖編譯簡單的代碼(比方說PrintFunctions.cpp):

clang++ -o PrintFunctions PrintFunctions.cpp 

,我得到的錯誤:

fatal error: 'clang/AST/ASTConsumer.h' file not found

嗯,我已經檢查了我的LLVM和鐺已經安裝好,並且文件「鐺/ AST/ASTConsumer.h」是

下找到

/usr/lib/llvm-3.4/include/clang/AST

所以,一定有一些我錯過了命令。我不知道該怎麼做......我已經在線閱讀了一些教程,其中大部分都使用了makefile,而且它們似乎很複雜。 那麼,如何編譯它呢?如何找到一個更簡單的方法來編寫makefile?

順便說一句,我在Ubuntu 14.04下,並且clang/LLVM版本是3.4。

回答

6

通常,在你的makefile,你應該有這樣的:

CXXFLAGS += `${LLVM_DIR}/bin/llvm-config --cxxflags` 

LDFLAGS += `${LLVM_DIR}/bin/llvm-config --ldflags` 
LLVMLIBS = `${LLVM_DIR}/bin/llvm-config --libs` 

[根據註釋編輯托馬斯]

您也可能使用:

LLVMLIBS += `${LLVM_DIR}/bin/llvm-config --system-libs` 

用於不屬於LLVM的部分,但LLVM依賴它。

[編輯完]

,然後使用這些:

.cpp.o: 
    ${CXX} ${CXXFLAGS} ${CXX_EXTRA} -c -o [email protected] $< 

myprog: ${OBJECTS} 
    ${LD} ${LDFLAGS} -o [email protected] ${OBJECTS} ${LIBS} 

(其中${OBJECTS}是你的目標文件的列表)

LLVM_DIR當然應該設置在哪裏指向您LLVM已安裝,例如/usr//usr/local - 運行which clang++以查看您的clang的安裝位置。

這是我的Makefile爲我的Pascal編譯器,使用LLVM作爲後端。

+1

LLVM 3.5+還附帶了可用於鏈接的'--system-libs'。 – Thomas

+0

@Thomas:是的,沒錯。 –